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