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