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