better support for mime/digest
[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 "html.h"
39 #include "enriched.h"
40 #include "codeconv.h"
41 #include "utils.h"
42 #include "prefs_common.h"
43
44 #if USE_GPGME
45 #  include "rfc2015.h"
46 #endif
47
48 static GHashTable *procmime_get_mime_type_table (void);
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         g_return_if_fail(parent != NULL);
138         g_return_if_fail(new->next == NULL);
139
140         for (child = parent->children; child && child != old;
141              child = child->next)
142                 ;
143         if (!child) {
144                 g_warning("oops: parent can't find it's own child");
145                 return;
146         }
147         procmime_mimeinfo_free_all(old);
148
149         if (child == parent->children) {
150                 new->next = parent->children->next;
151                 parent->children = new;
152         } else {
153                 new->next = child->next;
154                 child = new;
155         }
156 }
157
158 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
159 {
160         if (!mimeinfo) return NULL;
161
162         if (mimeinfo->children)
163                 return mimeinfo->children;
164         if (mimeinfo->sub)
165                 return mimeinfo->sub;
166         if (mimeinfo->next)
167                 return mimeinfo->next;
168
169         for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
170              mimeinfo = mimeinfo->parent) {
171                 if (mimeinfo->next)
172                         return mimeinfo->next;
173                 if (mimeinfo->main) {
174                         mimeinfo = mimeinfo->main;
175                         if (mimeinfo->next)
176                                 return mimeinfo->next;
177                 }
178         }
179
180         return NULL;
181 }
182
183 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
184 {
185         FILE *fp;
186         MimeInfo *mimeinfo;
187
188         g_return_val_if_fail(msginfo != NULL, NULL);
189
190         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
191         mimeinfo = procmime_scan_mime_header(fp, MIME_TEXT);
192
193         if (mimeinfo) {
194                 if (mimeinfo->mime_type != MIME_MULTIPART) {
195                         if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
196                                 perror("fseek");
197                 }
198                 if (mimeinfo->mime_type != MIME_TEXT)
199                         procmime_scan_multipart_message(mimeinfo, fp);
200         }
201
202 #if USE_GPGME
203         if (prefs_common.auto_check_signatures)
204                 rfc2015_check_signature(mimeinfo, fp);
205 #endif
206         fclose(fp);
207
208         return mimeinfo;
209 }
210
211 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
212 {
213         gchar *p;
214         gchar *boundary;
215         gint boundary_len = 0;
216         gchar buf[BUFFSIZE];
217         glong fpos, prev_fpos;
218         gint npart;
219         ContentType default_type;
220
221         g_return_if_fail(mimeinfo != NULL);
222         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
223
224         if (mimeinfo->mime_type == MIME_MULTIPART) {
225                 g_return_if_fail(mimeinfo->boundary != NULL);
226                 g_return_if_fail(mimeinfo->sub == NULL);
227         }
228         g_return_if_fail(fp != NULL);
229
230         boundary = mimeinfo->boundary;
231
232         if (boundary) {
233                 boundary_len = strlen(boundary);
234
235                 /* look for first boundary */
236                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
237                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
238                 if (!p) return;
239         }
240
241         if ((fpos = ftell(fp)) < 0) {
242                 perror("ftell");
243                 return;
244         }
245
246         /* Sets default subpart type for multipart/digest */
247         if (!strncasecmp(mimeinfo->content_type, "multipart/digest", 16))
248                 default_type = MIME_MESSAGE_RFC822;
249         else
250                 default_type = MIME_TEXT;
251
252         for (npart = 0;; npart++) {
253                 MimeInfo *partinfo;
254                 gboolean eom = FALSE;
255
256                 prev_fpos = fpos;
257
258                 partinfo = procmime_scan_mime_header(fp, default_type);
259                 if (!partinfo) break;
260                 procmime_mimeinfo_insert(mimeinfo, partinfo);
261
262                 if (partinfo->mime_type == MIME_MULTIPART) {
263                         if (partinfo->level < 8)
264                                 procmime_scan_multipart_message(partinfo, fp);
265                 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
266                         MimeInfo *sub;
267
268                         partinfo->sub = sub = procmime_scan_mime_header
269                                               (fp, MIME_TEXT);
270                         if (!sub) break;
271                         sub->level = partinfo->level + 1;
272                         sub->parent = partinfo;
273                         sub->main = partinfo;
274
275                         if (sub->mime_type == MIME_MULTIPART) {
276                                 if (sub->level < 8)
277                                         procmime_scan_multipart_message
278                                                 (sub, fp);
279                         }
280                 }
281
282                 /* look for next boundary */
283                 buf[0] = '\0';
284                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
285                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
286                                 if (buf[2 + boundary_len]     == '-' &&
287                                     buf[2 + boundary_len + 1] == '-')
288                                         eom = TRUE;
289                                 break;
290                         }
291                 }
292                 if (p == NULL)
293                         eom = TRUE;     /* broken MIME message */
294                 fpos = ftell(fp);
295
296                 partinfo->size = fpos - prev_fpos - strlen(buf);
297
298                 if (eom) break;
299         }
300 }
301
302 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
303 {
304         gchar *buf;
305
306         Xstrdup_a(buf, encoding, return);
307
308         g_free(mimeinfo->encoding);
309
310         mimeinfo->encoding = g_strdup(g_strstrip(buf));
311         if (!strcasecmp(buf, "7bit"))
312                 mimeinfo->encoding_type = ENC_7BIT;
313         else if (!strcasecmp(buf, "8bit"))
314                 mimeinfo->encoding_type = ENC_8BIT;
315         else if (!strcasecmp(buf, "quoted-printable"))
316                 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
317         else if (!strcasecmp(buf, "base64"))
318                 mimeinfo->encoding_type = ENC_BASE64;
319         else if (!strcasecmp(buf, "x-uuencode"))
320                 mimeinfo->encoding_type = ENC_X_UUENCODE;
321         else
322                 mimeinfo->encoding_type = ENC_UNKNOWN;
323
324 }
325
326 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
327 {
328         gchar *delim, *p, *cnttype;
329         gchar *buf;
330
331         if (conv_get_current_charset() == C_EUC_JP &&
332             strchr(content_type, '\033')) {
333                 gint len;
334                 len = strlen(content_type) * 2 + 1;
335                 Xalloca(buf, len, return);
336                 conv_jistoeuc(buf, len, content_type);
337         } else
338                 Xstrdup_a(buf, content_type, return);
339
340         g_free(mimeinfo->content_type);
341         g_free(mimeinfo->charset);
342         /* g_free(mimeinfo->name); */
343         mimeinfo->content_type = NULL;
344         mimeinfo->charset      = NULL;
345         /* mimeinfo->name      = NULL; */
346
347         if ((delim = strchr(buf, ';'))) *delim = '\0';
348         mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
349
350         mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
351
352         if (!delim) return;
353         p = delim + 1;
354
355         for (;;) {
356                 gchar *eq;
357                 gchar *attr, *value;
358
359                 if ((delim = strchr(p, ';'))) *delim = '\0';
360
361                 if (!(eq = strchr(p, '='))) break;
362
363                 *eq = '\0';
364                 attr = p;
365                 g_strstrip(attr);
366                 value = eq + 1;
367                 g_strstrip(value);
368
369                 if (*value == '"')
370                         extract_quote(value, '"');
371                 else {
372                         eliminate_parenthesis(value, '(', ')');
373                         g_strstrip(value);
374                 }
375
376                 if (*value) {
377                         if (!strcasecmp(attr, "charset"))
378                                 mimeinfo->charset = g_strdup(value);
379                         else if (!strcasecmp(attr, "name")) {
380                                 gchar *tmp;
381                                 size_t len;
382
383                                 len = strlen(value) + 1;
384                                 Xalloca(tmp, len, return);
385                                 conv_unmime_header(tmp, len, value, NULL);
386                                 g_free(mimeinfo->name);
387                                 mimeinfo->name = g_strdup(tmp);
388                         } else if (!strcasecmp(attr, "boundary"))
389                                 mimeinfo->boundary = g_strdup(value);
390                 }
391
392                 if (!delim) break;
393                 p = delim + 1;
394         }
395
396         if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
397                 mimeinfo->mime_type = MIME_TEXT;
398 }
399
400 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
401                                        const gchar *content_disposition)
402 {
403         gchar *delim, *p, *dispos;
404         gchar *buf;
405
406         if (conv_get_current_charset() == C_EUC_JP &&
407             strchr(content_disposition, '\033')) {
408                 gint len;
409                 len = strlen(content_disposition) * 2 + 1;
410                 Xalloca(buf, len, return);
411                 conv_jistoeuc(buf, len, content_disposition);
412         } else
413                 Xstrdup_a(buf, content_disposition, return);
414
415         if ((delim = strchr(buf, ';'))) *delim = '\0';
416         mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
417
418         if (!delim) return;
419         p = delim + 1;
420
421         for (;;) {
422                 gchar *eq;
423                 gchar *attr, *value;
424
425                 if ((delim = strchr(p, ';'))) *delim = '\0';
426
427                 if (!(eq = strchr(p, '='))) break;
428
429                 *eq = '\0';
430                 attr = p;
431                 g_strstrip(attr);
432                 value = eq + 1;
433                 g_strstrip(value);
434
435                 if (*value == '"')
436                         extract_quote(value, '"');
437                 else {
438                         eliminate_parenthesis(value, '(', ')');
439                         g_strstrip(value);
440                 }
441
442                 if (*value) {
443                         if (!strcasecmp(attr, "filename")) {
444                                 gchar *tmp;
445                                 size_t len;
446
447                                 len = strlen(value) + 1;
448                                 Xalloca(tmp, len, return);
449                                 conv_unmime_header(tmp, len, value, NULL);
450                                 g_free(mimeinfo->filename);
451                                 mimeinfo->filename = g_strdup(tmp);
452                                 break;
453                         }
454                 }
455
456                 if (!delim) break;
457                 p = delim + 1;
458         }
459 }
460
461 void procmime_scan_content_description(MimeInfo *mimeinfo,
462                                        const gchar *content_description)
463 {
464         gchar *delim, *p, *dispos;
465         gchar *buf;
466
467         gchar *tmp;
468         size_t blen;
469
470         if (conv_get_current_charset() == C_EUC_JP &&
471             strchr(content_description, '\033')) {
472                 gint len;
473                 len = strlen(content_description) * 2 + 1;
474                 Xalloca(buf, len, return);
475                 conv_jistoeuc(buf, len, content_description);
476         } else
477                 Xstrdup_a(buf, content_description, return);
478         
479         blen = strlen(buf) + 1;
480         Xalloca(tmp, blen, return);
481         conv_unmime_header(tmp, blen, buf, NULL);
482         g_free(mimeinfo->name);
483         mimeinfo->name = g_strdup(tmp);
484 }
485
486 void procmime_scan_subject(MimeInfo *mimeinfo,
487                            const gchar *subject)
488 {
489         gchar *delim, *p, *dispos;
490         gchar *buf;
491
492         gchar *tmp;
493         size_t blen;
494
495         if (conv_get_current_charset() == C_EUC_JP &&
496             strchr(subject, '\033')) {
497                 gint len;
498                 len = strlen(subject) * 2 + 1;
499                 Xalloca(buf, len, return);
500                 conv_jistoeuc(buf, len, subject);
501         } else
502                 Xstrdup_a(buf, subject, return);
503         
504         blen = strlen(buf) + 1;
505         Xalloca(tmp, blen, return);
506         conv_unmime_header(tmp, blen, buf, NULL);
507         g_free(mimeinfo->name);
508         mimeinfo->name = g_strdup(tmp);
509 }
510
511 enum
512 {
513         H_CONTENT_TRANSFER_ENCODING = 0,
514         H_CONTENT_TYPE              = 1,
515         H_CONTENT_DISPOSITION       = 2,
516         H_CONTENT_DESCRIPTION       = 3,
517         H_SUBJECT                   = 4
518 };
519
520 MimeInfo *procmime_scan_mime_header(FILE *fp, ContentType default_type)
521 {
522         static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
523                                                           NULL, FALSE},
524                                        {"Content-Type:", NULL, TRUE},
525                                        {"Content-Disposition:",
526                                                           NULL, TRUE},
527                                        {"Content-description:",
528                                                           NULL, TRUE},
529                                        {"Subject:",
530                                                           NULL, TRUE},
531                                        {NULL,             NULL, FALSE}};
532         gchar buf[BUFFSIZE];
533         gint hnum;
534         HeaderEntry *hp;
535         MimeInfo *mimeinfo;
536
537         g_return_val_if_fail(fp != NULL, NULL);
538
539         mimeinfo = procmime_mimeinfo_new();
540         /* Needed for multipart/digest */
541         mimeinfo->mime_type = default_type;
542         mimeinfo->encoding_type = ENC_7BIT;
543         mimeinfo->fpos = ftell(fp);
544
545         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
546                != -1) {
547                 hp = hentry + hnum;
548
549                 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
550                         procmime_scan_encoding
551                                 (mimeinfo, buf + strlen(hp->name));
552                 } else if (H_CONTENT_TYPE == hnum) {
553                         procmime_scan_content_type
554                                 (mimeinfo, buf + strlen(hp->name));
555                 } else if (H_CONTENT_DISPOSITION == hnum) {
556                         procmime_scan_content_disposition
557                                 (mimeinfo, buf + strlen(hp->name));
558                 } else if (H_CONTENT_DESCRIPTION == hnum) {
559                         procmime_scan_content_description
560                                 (mimeinfo, buf + strlen(hp->name));
561                 } else if (H_SUBJECT == hnum) {
562                         procmime_scan_subject
563                                 (mimeinfo, buf + strlen(hp->name));
564                 }
565         }
566
567         if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
568             mimeinfo->name) {
569                 const gchar *type;
570                 type = procmime_get_mime_type(mimeinfo->name);
571                 if (type)
572                         mimeinfo->mime_type = procmime_scan_mime_type(type);
573         }
574
575         if (!mimeinfo->content_type) {
576                 if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
577                         mimeinfo->content_type = g_strdup("message/rfc822");
578                 } else {
579                         mimeinfo->content_type = g_strdup("text/plain");
580                 }
581         }
582
583         return mimeinfo;
584 }
585
586 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
587 {
588         gchar buf[BUFFSIZE];
589         gchar *boundary = NULL;
590         gint boundary_len = 0;
591         gboolean tmp_file = FALSE;
592
593         g_return_val_if_fail(infp != NULL, NULL);
594         g_return_val_if_fail(mimeinfo != NULL, NULL);
595
596         if (!outfp) {
597                 outfp = my_tmpfile();
598                 if (!outfp) {
599                         perror("tmpfile");
600                         return NULL;
601                 }
602                 tmp_file = TRUE;
603         }
604
605         if (mimeinfo->parent && mimeinfo->parent->boundary) {
606                 boundary = mimeinfo->parent->boundary;
607                 boundary_len = strlen(boundary);
608         }
609
610         if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
611                 gboolean softline = FALSE;
612
613                 while (fgets(buf, sizeof(buf), infp) != NULL &&
614                        (!boundary ||
615                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
616                         guchar *p = buf;
617
618                         softline = DoOneQPLine(&p, FALSE, softline);
619                         fwrite(buf, p - (guchar *)buf, 1, outfp);
620                 }
621         } else if (mimeinfo->encoding_type == ENC_BASE64) {
622                 gchar outbuf[BUFFSIZE];
623                 gint len;
624                 Base64Decoder *decoder;
625
626                 decoder = base64_decoder_new();
627                 while (fgets(buf, sizeof(buf), infp) != NULL &&
628                        (!boundary ||
629                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
630                         len = base64_decoder_decode(decoder, buf, outbuf);
631                         if (len < 0) {
632                                 g_warning("Bad BASE64 content\n");
633                                 break;
634                         }
635                         fwrite(outbuf, sizeof(gchar), len, outfp);
636                 }
637                 base64_decoder_free(decoder);
638         } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
639                 gchar outbuf[BUFFSIZE];
640                 gint len;
641                 gboolean flag = FALSE;
642
643                 while (fgets(buf, sizeof(buf), infp) != NULL &&
644                        (!boundary ||
645                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
646                         if(!flag && strncmp(buf,"begin ", 6)) continue;
647
648                         if (flag) {
649                                 len = fromuutobits(outbuf, buf);
650                                 if (len <= 0) {
651                                         if (len < 0) 
652                                                 g_warning("Bad UUENCODE content(%d)\n", len);
653                                         break;
654                                 }
655                                 fwrite(outbuf, sizeof(gchar), len, outfp);
656                         } else
657                                 flag = TRUE;
658                 }
659         } else {
660                 while (fgets(buf, sizeof(buf), infp) != NULL &&
661                        (!boundary ||
662                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
663                         fputs(buf, outfp);
664                 }
665         }
666
667         if (tmp_file) rewind(outfp);
668         return outfp;
669 }
670
671 gint procmime_get_part(const gchar *outfile, const gchar *infile,
672                        MimeInfo *mimeinfo)
673 {
674         FILE *infp, *outfp;
675         gchar buf[BUFFSIZE];
676
677         g_return_val_if_fail(outfile != NULL, -1);
678         g_return_val_if_fail(infile != NULL, -1);
679         g_return_val_if_fail(mimeinfo != NULL, -1);
680
681         if ((infp = fopen(infile, "r")) == NULL) {
682                 FILE_OP_ERROR(infile, "fopen");
683                 return -1;
684         }
685         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
686                 FILE_OP_ERROR(infile, "fseek");
687                 fclose(infp);
688                 return -1;
689         }
690         if ((outfp = fopen(outfile, "w")) == NULL) {
691                 FILE_OP_ERROR(outfile, "fopen");
692                 fclose(infp);
693                 return -1;
694         }
695
696         while (fgets(buf, sizeof(buf), infp) != NULL)
697                 if (buf[0] == '\r' || buf[0] == '\n') break;
698
699         procmime_decode_content(outfp, infp, mimeinfo);
700
701         fclose(infp);
702         if (fclose(outfp) == EOF) {
703                 FILE_OP_ERROR(outfile, "fclose");
704                 unlink(outfile);
705                 return -1;
706         }
707
708         return 0;
709 }
710
711 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
712 {
713         FILE *tmpfp, *outfp;
714         gchar *src_codeset;
715         gboolean conv_fail = FALSE;
716         gchar buf[BUFFSIZE];
717         gchar *str;
718
719         g_return_val_if_fail(mimeinfo != NULL, NULL);
720         g_return_val_if_fail(infp != NULL, NULL);
721         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
722                              mimeinfo->mime_type == MIME_TEXT_HTML ||
723                              mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
724
725         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
726                 perror("fseek");
727                 return NULL;
728         }
729
730         while (fgets(buf, sizeof(buf), infp) != NULL)
731                 if (buf[0] == '\r' || buf[0] == '\n') break;
732
733         tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
734         if (!tmpfp)
735                 return NULL;
736
737         if ((outfp = my_tmpfile()) == NULL) {
738                 perror("tmpfile");
739                 fclose(tmpfp);
740                 return NULL;
741         }
742
743         src_codeset = prefs_common.force_charset
744                 ? prefs_common.force_charset : mimeinfo->charset;
745
746         if (mimeinfo->mime_type == MIME_TEXT) {
747                 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
748                         str = conv_codeset_strdup(buf, src_codeset, NULL);
749                         if (str) {
750                                 fputs(str, outfp);
751                                 g_free(str);
752                         } else {
753                                 conv_fail = TRUE;
754                                 fputs(buf, outfp);
755                         }
756                 }
757         } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
758                 HTMLParser *parser;
759                 CodeConverter *conv;
760
761                 conv = conv_code_converter_new(src_codeset);
762                 parser = html_parser_new(tmpfp, conv);
763                 while ((str = html_parse(parser)) != NULL) {
764                         fputs(str, outfp);
765                 }
766                 html_parser_destroy(parser);
767                 conv_code_converter_destroy(conv);
768         } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
769                 ERTFParser *parser;
770                 CodeConverter *conv;
771
772                 conv = conv_code_converter_new(src_codeset);
773                 parser = ertf_parser_new(tmpfp, conv);
774                 while ((str = ertf_parse(parser)) != NULL) {
775                         fputs(str, outfp);
776                 }
777                 ertf_parser_destroy(parser);
778                 conv_code_converter_destroy(conv);
779         }
780
781         if (conv_fail)
782                 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
783
784         fclose(tmpfp);
785         rewind(outfp);
786
787         return outfp;
788 }
789
790 /* search the first text part of (multipart) MIME message,
791    decode, convert it and output to outfp. */
792 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
793 {
794         FILE *infp, *outfp = NULL;
795         MimeInfo *mimeinfo, *partinfo;
796
797         g_return_val_if_fail(msginfo != NULL, NULL);
798
799         mimeinfo = procmime_scan_message(msginfo);
800         if (!mimeinfo) return NULL;
801
802         if ((infp = procmsg_open_message(msginfo)) == NULL) {
803                 procmime_mimeinfo_free_all(mimeinfo);
804                 return NULL;
805         }
806
807         partinfo = mimeinfo;
808         while (partinfo && partinfo->mime_type != MIME_TEXT)
809                 partinfo = procmime_mimeinfo_next(partinfo);
810         if (!partinfo) {
811                 partinfo = mimeinfo;
812                 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
813                                 partinfo->mime_type != MIME_TEXT_ENRICHED)
814                         partinfo = procmime_mimeinfo_next(partinfo);
815         }
816         
817
818         if (partinfo)
819                 outfp = procmime_get_text_content(partinfo, infp);
820
821         fclose(infp);
822         procmime_mimeinfo_free_all(mimeinfo);
823
824         return outfp;
825 }
826
827 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
828                                    const gchar *str, gboolean case_sens)
829 {
830
831         FILE *infp, *outfp;
832         gchar buf[BUFFSIZE];
833         gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
834
835         g_return_val_if_fail(mimeinfo != NULL, FALSE);
836         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
837                              mimeinfo->mime_type == MIME_TEXT_HTML ||
838                              mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
839         g_return_val_if_fail(str != NULL, FALSE);
840
841         if ((infp = fopen(filename, "r")) == NULL) {
842                 FILE_OP_ERROR(filename, "fopen");
843                 return FALSE;
844         }
845
846         outfp = procmime_get_text_content(mimeinfo, infp);
847         fclose(infp);
848
849         if (!outfp)
850                 return FALSE;
851
852         if (case_sens)
853                 StrFindFunc = strstr;
854         else
855                 StrFindFunc = strcasestr;
856
857         while (fgets(buf, sizeof(buf), outfp) != NULL) {
858                 if (StrFindFunc(buf, str) != NULL) {
859                         fclose(outfp);
860                         return TRUE;
861                 }
862         }
863
864         fclose(outfp);
865
866         return FALSE;
867 }
868
869 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
870                               gboolean case_sens)
871 {
872         MimeInfo *mimeinfo;
873         MimeInfo *partinfo;
874         gchar *filename;
875         gboolean found = FALSE;
876
877         g_return_val_if_fail(msginfo != NULL, FALSE);
878         g_return_val_if_fail(str != NULL, FALSE);
879
880         filename = procmsg_get_message_file(msginfo);
881         if (!filename) return FALSE;
882         mimeinfo = procmime_scan_message(msginfo);
883
884         for (partinfo = mimeinfo; partinfo != NULL;
885              partinfo = procmime_mimeinfo_next(partinfo)) {
886                 if (partinfo->mime_type == MIME_TEXT ||
887                     partinfo->mime_type == MIME_TEXT_HTML ||
888                     partinfo->mime_type == MIME_TEXT_ENRICHED) {
889                         if (procmime_find_string_part
890                                 (partinfo, filename, str, case_sens) == TRUE) {
891                                 found = TRUE;
892                                 break;
893                         }
894                 }
895         }
896
897         procmime_mimeinfo_free_all(mimeinfo);
898         g_free(filename);
899
900         return found;
901 }
902
903 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
904 {
905         static guint32 id = 0;
906         gchar *base;
907         gchar *filename;
908         gchar f_prefix[10];
909
910         g_return_val_if_fail(mimeinfo != NULL, NULL);
911
912         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
913
914         if (MIME_TEXT_HTML == mimeinfo->mime_type)
915                 base = "mimetmp.html";
916         else {
917                 base = mimeinfo->filename ? mimeinfo->filename
918                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
919                 base = g_basename(base);
920                 if (*base == '\0') base = "mimetmp";
921         }
922
923         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
924                                f_prefix, base, NULL);
925
926         return filename;
927 }
928
929 ContentType procmime_scan_mime_type(const gchar *mime_type)
930 {
931         ContentType type;
932
933         if (!strncasecmp(mime_type, "text/html", 9))
934                 type = MIME_TEXT_HTML;
935         else if (!strncasecmp(mime_type, "text/enriched", 13))
936                 type = MIME_TEXT_ENRICHED;
937         else if (!strncasecmp(mime_type, "text/", 5))
938                 type = MIME_TEXT;
939         else if (!strncasecmp(mime_type, "message/rfc822", 14))
940                 type = MIME_MESSAGE_RFC822;
941         else if (!strncasecmp(mime_type, "message/", 8))
942                 type = MIME_TEXT;
943         else if (!strncasecmp(mime_type, "application/octet-stream", 24))
944                 type = MIME_APPLICATION_OCTET_STREAM;
945         else if (!strncasecmp(mime_type, "application/", 12))
946                 type = MIME_APPLICATION;
947         else if (!strncasecmp(mime_type, "multipart/", 10))
948                 type = MIME_MULTIPART;
949         else if (!strncasecmp(mime_type, "image/", 6))
950                 type = MIME_IMAGE;
951         else if (!strncasecmp(mime_type, "audio/", 6))
952                 type = MIME_AUDIO;
953         else if (!strcasecmp(mime_type, "text"))
954                 type = MIME_TEXT;
955         else
956                 type = MIME_UNKNOWN;
957
958         return type;
959 }
960
961 static GList *mime_type_list = NULL;
962
963 gchar *procmime_get_mime_type(const gchar *filename)
964 {
965         static GHashTable *mime_type_table = NULL;
966         MimeType *mime_type;
967         const gchar *ext, *p;
968
969         if (!mime_type_table) {
970                 mime_type_table = procmime_get_mime_type_table();
971                 if (!mime_type_table) return NULL;
972         }
973
974         filename = g_basename(filename);
975         p = strrchr(filename, '.');
976         if (p)
977                 ext = p + 1;
978         else
979                 return NULL;
980
981         mime_type = g_hash_table_lookup(mime_type_table, ext);
982         if (mime_type) {
983                 gchar *str;
984
985                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
986                                   NULL);
987                 return str;
988         }
989
990         return NULL;
991 }
992
993 static guint procmime_str_hash(gconstpointer gptr)
994 {
995         guint hash_result = 0;
996         const char *str;
997
998         for (str = gptr; str && *str; str++) {
999                 if (isupper(*str)) hash_result += (*str + ' ');
1000                 else hash_result += *str;
1001         }
1002
1003         return hash_result;
1004 }
1005
1006 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1007 {
1008         const char *str1 = gptr1;
1009         const char *str2 = gptr2;
1010
1011         return !strcasecmp(str1, str2);
1012 }
1013
1014 static GHashTable *procmime_get_mime_type_table(void)
1015 {
1016         GHashTable *table = NULL;
1017         GList *cur;
1018         MimeType *mime_type;
1019         gchar **exts;
1020
1021         if (!mime_type_list) {
1022                 mime_type_list = procmime_get_mime_type_list();
1023                 if (!mime_type_list) return NULL;
1024         }
1025
1026         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1027
1028         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1029                 gint i;
1030
1031                 mime_type = (MimeType *)cur->data;
1032
1033                 if (!mime_type->extension) continue;
1034
1035                 exts = g_strsplit(mime_type->extension, " ", 16);
1036                 for (i = 0; exts[i] != NULL; i++)
1037                         g_hash_table_insert(table, g_strdup(exts[i]),
1038                                             mime_type);
1039                 g_strfreev(exts);
1040         }
1041
1042         return table;
1043 }
1044
1045 GList *procmime_get_mime_type_list(void)
1046 {
1047         GList *list = NULL;
1048         FILE *fp;
1049         gchar buf[BUFFSIZE];
1050         gchar *p, *delim;
1051         MimeType *mime_type;
1052
1053         if (mime_type_list) 
1054                 return mime_type_list;
1055
1056         if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
1057                 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
1058                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1059                         return NULL;
1060                 }
1061         }
1062
1063         while (fgets(buf, sizeof(buf), fp) != NULL) {
1064                 p = strchr(buf, '#');
1065                 if (p) *p = '\0';
1066                 g_strstrip(buf);
1067
1068                 p = buf;
1069                 while (*p && !isspace(*p)) p++;
1070                 if (*p) {
1071                         *p = '\0';
1072                         p++;
1073                 }
1074                 delim = strchr(buf, '/');
1075                 if (delim == NULL) continue;
1076                 *delim = '\0';
1077
1078                 mime_type = g_new(MimeType, 1);
1079                 mime_type->type = g_strdup(buf);
1080                 mime_type->sub_type = g_strdup(delim + 1);
1081
1082                 while (*p && isspace(*p)) p++;
1083                 if (*p)
1084                         mime_type->extension = g_strdup(p);
1085                 else
1086                         mime_type->extension = NULL;
1087
1088                 list = g_list_append(list, mime_type);
1089         }
1090
1091         fclose(fp);
1092
1093         if (!list)
1094                 g_warning("Can't read mime.types\n");
1095
1096         return list;
1097 }
1098
1099 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1100 {
1101         if (!charset)
1102                 return ENC_8BIT;
1103         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1104                  !strcasecmp(charset, "US-ASCII"))
1105                 return ENC_7BIT;
1106         else
1107                 return ENC_8BIT;
1108                 /* return ENC_BASE64; */
1109                 /* return ENC_QUOTED_PRINTABLE; */
1110 }
1111
1112 const gchar *procmime_get_encoding_str(EncodingType encoding)
1113 {
1114         static const gchar *encoding_str[] = {
1115                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1116                 NULL
1117         };
1118
1119         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1120                 return encoding_str[encoding];
1121         else
1122                 return NULL;
1123 }