fixed local account
[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                 renderer_list = g_list_append(renderer_list, cr);
760         }
761
762         fclose(f);
763 }
764
765 void renderer_write_config(void)
766 {
767         int i;
768         gchar * rcpath;
769         PrefFile *pfile;
770         GList * cur;
771
772         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
773         
774         if ((pfile = prefs_write_open(rcpath)) == NULL) {
775                 g_warning(_("failed to write configuration to file\n"));
776                 g_free(rcpath);
777                 return;
778         }
779
780         g_free(rcpath);
781
782         for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
783                 struct ContentRenderer * renderer;
784                 renderer = cur->data;
785                 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
786                         renderer->renderer);
787         }
788
789         if (prefs_write_close(pfile) < 0) {
790                 g_warning(_("failed to write configuration to file\n"));
791                 return;
792         }
793 }
794
795 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
796 {
797         FILE *tmpfp, *outfp;
798         gchar *src_codeset;
799         gboolean conv_fail = FALSE;
800         gchar buf[BUFFSIZE];
801         gchar *str;
802         int i;
803         struct ContentRenderer * renderer;
804         GList * cur;
805
806         g_return_val_if_fail(mimeinfo != NULL, NULL);
807         g_return_val_if_fail(infp != NULL, NULL);
808         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
809                              mimeinfo->mime_type == MIME_TEXT_HTML ||
810                              mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
811
812         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
813                 perror("fseek");
814                 return NULL;
815         }
816
817         while (fgets(buf, sizeof(buf), infp) != NULL)
818                 if (buf[0] == '\r' || buf[0] == '\n') break;
819
820         tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
821         if (!tmpfp)
822                 return NULL;
823
824         if ((outfp = my_tmpfile()) == NULL) {
825                 perror("tmpfile");
826                 fclose(tmpfp);
827                 return NULL;
828         }
829
830         src_codeset = prefs_common.force_charset
831                 ? prefs_common.force_charset : mimeinfo->charset;
832
833         renderer = NULL;
834
835         for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
836                 struct ContentRenderer * cr;
837                 cr = cur->data;
838                 if (g_strcasecmp(cr->content_type,
839                                  mimeinfo->content_type) == 0) {
840                         renderer = cr;
841                         break;
842                 }
843         }
844
845         if (renderer != NULL) {
846                 FILE * p;
847                 int oldout;
848                 
849                 oldout = dup(1);
850                 
851                 dup2(fileno(outfp), 1);
852                 
853                 p = popen(renderer->renderer, "w");
854                 if (p != NULL) {
855                         size_t count;
856                         
857                         while ((count =
858                                 fread(buf, sizeof(char), sizeof(buf),
859                                       tmpfp)) > 0)
860                                 fwrite(buf, sizeof(char), count, p);
861                         pclose(p);
862                 }
863                 
864                 dup2(oldout, 1);
865         } else if (mimeinfo->mime_type == MIME_TEXT) {
866                 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
867                         str = conv_codeset_strdup(buf, src_codeset, NULL);
868                         if (str) {
869                                 fputs(str, outfp);
870                                 g_free(str);
871                         } else {
872                                 conv_fail = TRUE;
873                                 fputs(buf, outfp);
874                         }
875                 }
876         } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
877                 HTMLParser *parser;
878                 CodeConverter *conv;
879
880                 conv = conv_code_converter_new(src_codeset);
881                 parser = html_parser_new(tmpfp, conv);
882                 while ((str = html_parse(parser)) != NULL) {
883                         fputs(str, outfp);
884                 }
885                 html_parser_destroy(parser);
886                 conv_code_converter_destroy(conv);
887         } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
888                 ERTFParser *parser;
889                 CodeConverter *conv;
890
891                 conv = conv_code_converter_new(src_codeset);
892                 parser = ertf_parser_new(tmpfp, conv);
893                 while ((str = ertf_parse(parser)) != NULL) {
894                         fputs(str, outfp);
895                 }
896                 ertf_parser_destroy(parser);
897                 conv_code_converter_destroy(conv);
898         }
899
900         if (conv_fail)
901                 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
902
903         fclose(tmpfp);
904         rewind(outfp);
905
906         return outfp;
907 }
908
909 /* search the first text part of (multipart) MIME message,
910    decode, convert it and output to outfp. */
911 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
912 {
913         FILE *infp, *outfp = NULL;
914         MimeInfo *mimeinfo, *partinfo;
915
916         g_return_val_if_fail(msginfo != NULL, NULL);
917
918         mimeinfo = procmime_scan_message(msginfo);
919         if (!mimeinfo) return NULL;
920
921         if ((infp = procmsg_open_message(msginfo)) == NULL) {
922                 procmime_mimeinfo_free_all(mimeinfo);
923                 return NULL;
924         }
925
926         partinfo = mimeinfo;
927         while (partinfo && partinfo->mime_type != MIME_TEXT)
928                 partinfo = procmime_mimeinfo_next(partinfo);
929         if (!partinfo) {
930                 partinfo = mimeinfo;
931                 while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
932                                 partinfo->mime_type != MIME_TEXT_ENRICHED)
933                         partinfo = procmime_mimeinfo_next(partinfo);
934         }
935         
936
937         if (partinfo)
938                 outfp = procmime_get_text_content(partinfo, infp);
939
940         fclose(infp);
941         procmime_mimeinfo_free_all(mimeinfo);
942
943         return outfp;
944 }
945
946 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
947                                    const gchar *str, gboolean case_sens)
948 {
949
950         FILE *infp, *outfp;
951         gchar buf[BUFFSIZE];
952         gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
953
954         g_return_val_if_fail(mimeinfo != NULL, FALSE);
955         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
956                              mimeinfo->mime_type == MIME_TEXT_HTML ||
957                              mimeinfo->mime_type == MIME_TEXT_ENRICHED, FALSE);
958         g_return_val_if_fail(str != NULL, FALSE);
959
960         if ((infp = fopen(filename, "r")) == NULL) {
961                 FILE_OP_ERROR(filename, "fopen");
962                 return FALSE;
963         }
964
965         outfp = procmime_get_text_content(mimeinfo, infp);
966         fclose(infp);
967
968         if (!outfp)
969                 return FALSE;
970
971         if (case_sens)
972                 StrFindFunc = strstr;
973         else
974                 StrFindFunc = strcasestr;
975
976         while (fgets(buf, sizeof(buf), outfp) != NULL) {
977                 if (StrFindFunc(buf, str) != NULL) {
978                         fclose(outfp);
979                         return TRUE;
980                 }
981         }
982
983         fclose(outfp);
984
985         return FALSE;
986 }
987
988 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
989                               gboolean case_sens)
990 {
991         MimeInfo *mimeinfo;
992         MimeInfo *partinfo;
993         gchar *filename;
994         gboolean found = FALSE;
995
996         g_return_val_if_fail(msginfo != NULL, FALSE);
997         g_return_val_if_fail(str != NULL, FALSE);
998
999         filename = procmsg_get_message_file(msginfo);
1000         if (!filename) return FALSE;
1001         mimeinfo = procmime_scan_message(msginfo);
1002
1003         for (partinfo = mimeinfo; partinfo != NULL;
1004              partinfo = procmime_mimeinfo_next(partinfo)) {
1005                 if (partinfo->mime_type == MIME_TEXT ||
1006                     partinfo->mime_type == MIME_TEXT_HTML ||
1007                     partinfo->mime_type == MIME_TEXT_ENRICHED) {
1008                         if (procmime_find_string_part
1009                                 (partinfo, filename, str, case_sens) == TRUE) {
1010                                 found = TRUE;
1011                                 break;
1012                         }
1013                 }
1014         }
1015
1016         procmime_mimeinfo_free_all(mimeinfo);
1017         g_free(filename);
1018
1019         return found;
1020 }
1021
1022 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
1023 {
1024         static guint32 id = 0;
1025         gchar *base;
1026         gchar *filename;
1027         gchar f_prefix[10];
1028
1029         g_return_val_if_fail(mimeinfo != NULL, NULL);
1030
1031         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
1032
1033         if (MIME_TEXT_HTML == mimeinfo->mime_type)
1034                 base = "mimetmp.html";
1035         else {
1036                 base = mimeinfo->filename ? mimeinfo->filename
1037                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
1038                 base = g_basename(base);
1039                 if (*base == '\0') base = "mimetmp";
1040         }
1041
1042         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
1043                                f_prefix, base, NULL);
1044
1045         return filename;
1046 }
1047
1048 ContentType procmime_scan_mime_type(const gchar *mime_type)
1049 {
1050         ContentType type;
1051
1052         if (!strncasecmp(mime_type, "text/html", 9))
1053                 type = MIME_TEXT_HTML;
1054         else if (!strncasecmp(mime_type, "text/enriched", 13))
1055                 type = MIME_TEXT_ENRICHED;
1056         else if (!strncasecmp(mime_type, "text/", 5))
1057                 type = MIME_TEXT;
1058         else if (!strncasecmp(mime_type, "message/rfc822", 14))
1059                 type = MIME_MESSAGE_RFC822;
1060         else if (!strncasecmp(mime_type, "message/", 8))
1061                 type = MIME_TEXT;
1062         else if (!strncasecmp(mime_type, "application/octet-stream", 24))
1063                 type = MIME_APPLICATION_OCTET_STREAM;
1064         else if (!strncasecmp(mime_type, "application/", 12))
1065                 type = MIME_APPLICATION;
1066         else if (!strncasecmp(mime_type, "multipart/", 10))
1067                 type = MIME_MULTIPART;
1068         else if (!strncasecmp(mime_type, "image/", 6))
1069                 type = MIME_IMAGE;
1070         else if (!strncasecmp(mime_type, "audio/", 6))
1071                 type = MIME_AUDIO;
1072         else if (!strcasecmp(mime_type, "text"))
1073                 type = MIME_TEXT;
1074         else
1075                 type = MIME_UNKNOWN;
1076
1077         return type;
1078 }
1079
1080 static GList *mime_type_list = NULL;
1081
1082 gchar *procmime_get_mime_type(const gchar *filename)
1083 {
1084         static GHashTable *mime_type_table = NULL;
1085         MimeType *mime_type;
1086         const gchar *p;
1087         gchar *ext;
1088
1089         if (!mime_type_table) {
1090                 mime_type_table = procmime_get_mime_type_table();
1091                 if (!mime_type_table) return NULL;
1092         }
1093
1094         filename = g_basename(filename);
1095         p = strrchr(filename, '.');
1096         if (!p) return NULL;
1097
1098         Xstrdup_a(ext, p + 1, return NULL);
1099         g_strdown(ext);
1100         mime_type = g_hash_table_lookup(mime_type_table, ext);
1101         if (mime_type) {
1102                 gchar *str;
1103
1104                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
1105                                   NULL);
1106                 return str;
1107         }
1108
1109         return NULL;
1110 }
1111
1112 static guint procmime_str_hash(gconstpointer gptr)
1113 {
1114         guint hash_result = 0;
1115         const char *str;
1116
1117         for (str = gptr; str && *str; str++) {
1118                 if (isupper(*str)) hash_result += (*str + ' ');
1119                 else hash_result += *str;
1120         }
1121
1122         return hash_result;
1123 }
1124
1125 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
1126 {
1127         const char *str1 = gptr1;
1128         const char *str2 = gptr2;
1129
1130         return !strcasecmp(str1, str2);
1131 }
1132
1133 static GHashTable *procmime_get_mime_type_table(void)
1134 {
1135         GHashTable *table = NULL;
1136         GList *cur;
1137         MimeType *mime_type;
1138         gchar **exts;
1139
1140         if (!mime_type_list) {
1141                 mime_type_list = procmime_get_mime_type_list();
1142                 if (!mime_type_list) return NULL;
1143         }
1144
1145         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
1146
1147         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
1148                 gint i;
1149                 gchar *key;
1150
1151                 mime_type = (MimeType *)cur->data;
1152
1153                 if (!mime_type->extension) continue;
1154
1155                 exts = g_strsplit(mime_type->extension, " ", 16);
1156                 for (i = 0; exts[i] != NULL; i++) {
1157                         /* make the key case insensitive */
1158                         g_strdown(exts[i]);
1159                         /* use previously dup'd key on overwriting */
1160                         if (g_hash_table_lookup(table, exts[i]))
1161                                 key = exts[i];
1162                         else
1163                                 key = g_strdup(exts[i]);
1164                         g_hash_table_insert(table, key, mime_type);
1165                 }
1166                 g_strfreev(exts);
1167         }
1168
1169         return table;
1170 }
1171
1172 GList *procmime_get_mime_type_list(void)
1173 {
1174         GList *list = NULL;
1175         FILE *fp;
1176         gchar buf[BUFFSIZE];
1177         gchar *p, *delim;
1178         MimeType *mime_type;
1179
1180         if (mime_type_list) 
1181                 return mime_type_list;
1182
1183         if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
1184                 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
1185                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1186                         return NULL;
1187                 }
1188         }
1189
1190         while (fgets(buf, sizeof(buf), fp) != NULL) {
1191                 p = strchr(buf, '#');
1192                 if (p) *p = '\0';
1193                 g_strstrip(buf);
1194
1195                 p = buf;
1196                 while (*p && !isspace(*p)) p++;
1197                 if (*p) {
1198                         *p = '\0';
1199                         p++;
1200                 }
1201                 delim = strchr(buf, '/');
1202                 if (delim == NULL) continue;
1203                 *delim = '\0';
1204
1205                 mime_type = g_new(MimeType, 1);
1206                 mime_type->type = g_strdup(buf);
1207                 mime_type->sub_type = g_strdup(delim + 1);
1208
1209                 while (*p && isspace(*p)) p++;
1210                 if (*p)
1211                         mime_type->extension = g_strdup(p);
1212                 else
1213                         mime_type->extension = NULL;
1214
1215                 list = g_list_append(list, mime_type);
1216         }
1217
1218         fclose(fp);
1219
1220         if (!list)
1221                 g_warning("Can't read mime.types\n");
1222
1223         return list;
1224 }
1225
1226 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1227 {
1228         if (!charset)
1229                 return ENC_8BIT;
1230         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1231                  !strcasecmp(charset, "US-ASCII"))
1232                 return ENC_7BIT;
1233         else
1234                 return ENC_8BIT;
1235                 /* return ENC_BASE64; */
1236                 /* return ENC_QUOTED_PRINTABLE; */
1237 }
1238
1239 const gchar *procmime_get_encoding_str(EncodingType encoding)
1240 {
1241         static const gchar *encoding_str[] = {
1242                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1243                 NULL
1244         };
1245
1246         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1247                 return encoding_str[encoding];
1248         else
1249                 return NULL;
1250 }