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