2005-03-18 [paul] 1.0.3cvs2.4
[claws.git] / src / procmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
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 <stdio.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <locale.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "procmime.h"
38 #include "procheader.h"
39 #include "base64.h"
40 #include "quoted-printable.h"
41 #include "uuencode.h"
42 #include "unmime.h"
43 #include "html.h"
44 #include "enriched.h"
45 #include "codeconv.h"
46 #include "utils.h"
47 #include "prefs_common.h"
48 #include "prefs_gtk.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->content        = MIMECONTENT_EMPTY;
58         mimeinfo->data.filename  = NULL;
59
60         mimeinfo->type           = MIMETYPE_UNKNOWN;
61         mimeinfo->encoding_type  = ENC_UNKNOWN;
62         mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
63
64         mimeinfo->disposition    = DISPOSITIONTYPE_UNKNOWN;
65         mimeinfo->dispositionparameters 
66                                  = g_hash_table_new(g_str_hash, g_str_equal);
67
68         mimeinfo->node           = g_node_new(mimeinfo);
69         
70         return mimeinfo;
71 }
72
73 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
74 {
75         g_free(key);
76         g_free(value);
77         
78         return TRUE;
79 }
80
81 static gchar *forced_charset = NULL;
82
83 void procmime_force_charset(const gchar *str)
84 {
85         g_free(forced_charset);
86         forced_charset = NULL;
87         if (str)
88                 forced_charset = g_strdup(str);
89 }
90
91 static EncodingType forced_encoding = 0;
92
93 void procmime_force_encoding(EncodingType encoding)
94 {
95         forced_encoding = encoding;
96 }
97
98 static gboolean free_func(GNode *node, gpointer data)
99 {
100         MimeInfo *mimeinfo = (MimeInfo *) node->data;
101
102         switch (mimeinfo->content) {
103         case MIMECONTENT_FILE:
104                 if (mimeinfo->tmp)
105                         unlink(mimeinfo->data.filename);
106                 g_free(mimeinfo->data.filename);
107                 break;
108
109         case MIMECONTENT_MEM:
110                 if (mimeinfo->tmp)
111                         g_free(mimeinfo->data.mem);
112         default:
113                 break;
114         }
115
116         g_free(mimeinfo->subtype);
117         g_free(mimeinfo->description);
118         g_free(mimeinfo->id);
119
120         g_hash_table_foreach_remove(mimeinfo->typeparameters,
121                 procmime_mimeinfo_parameters_destroy, NULL);
122         g_hash_table_destroy(mimeinfo->typeparameters);
123         g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
124                 procmime_mimeinfo_parameters_destroy, NULL);
125         g_hash_table_destroy(mimeinfo->dispositionparameters);
126
127         if (mimeinfo->privacy)
128                 privacy_free_privacydata(mimeinfo->privacy);
129
130         g_free(mimeinfo);
131
132         return FALSE;
133 }
134
135 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
136 {
137         GNode *node;
138
139         if (!mimeinfo)
140                 return;
141
142         node = mimeinfo->node;
143         g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
144
145         g_node_destroy(node);
146 }
147
148 #if 0 /* UNUSED */
149 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
150 {
151         MimeInfo *child = parent->children;
152
153         if (!child)
154                 parent->children = mimeinfo;
155         else {
156                 while (child->next != NULL)
157                         child = child->next;
158
159                 child->next = mimeinfo;
160         }
161
162         mimeinfo->parent = parent;
163         mimeinfo->level = parent->level + 1;
164
165         return mimeinfo;
166 }
167
168 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
169 {
170         MimeInfo *parent = old->parent;
171         MimeInfo *child;
172
173         g_return_if_fail(parent != NULL);
174         g_return_if_fail(new->next == NULL);
175
176         for (child = parent->children; child && child != old;
177              child = child->next)
178                 ;
179         if (!child) {
180                 g_warning("oops: parent can't find it's own child");
181                 return;
182         }
183         procmime_mimeinfo_free_all(old);
184
185         if (child == parent->children) {
186                 new->next = parent->children->next;
187                 parent->children = new;
188         } else {
189                 new->next = child->next;
190                 child = new;
191         }
192 }
193 #endif
194
195 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
196 {
197         g_return_val_if_fail(mimeinfo != NULL, NULL);
198         g_return_val_if_fail(mimeinfo->node != NULL, NULL);
199
200         if (mimeinfo->node->parent == NULL)
201                 return NULL;
202         return (MimeInfo *) mimeinfo->node->parent->data;
203 }
204
205 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
206 {
207         g_return_val_if_fail(mimeinfo != NULL, NULL);
208         g_return_val_if_fail(mimeinfo->node != NULL, NULL);
209
210         if (mimeinfo->node->children)
211                 return (MimeInfo *) mimeinfo->node->children->data;
212         if (mimeinfo->node->next)
213                 return (MimeInfo *) mimeinfo->node->next->data;
214
215         if (mimeinfo->node->parent == NULL)
216                 return NULL;
217
218         while (mimeinfo->node->parent != NULL) {
219                 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
220                 if (mimeinfo->node->next)
221                         return (MimeInfo *) mimeinfo->node->next->data;
222         }
223
224         return NULL;
225 }
226
227 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
228 {
229         gchar *filename;
230         MimeInfo *mimeinfo;
231
232         filename = procmsg_get_message_file_path(msginfo);
233         if (!filename || !is_file_exist(filename)) {
234                 g_free(filename);
235                 filename = procmsg_get_message_file(msginfo);
236         }
237         if (!filename || !is_file_exist(filename)) 
238                 return NULL;
239
240         if (msginfo->folder->stype != F_QUEUE && 
241             msginfo->folder->stype != F_DRAFT)
242                 mimeinfo = procmime_scan_file(filename);
243         else
244                 mimeinfo = procmime_scan_queue_file(filename);
245         g_free(filename);
246
247         return mimeinfo;
248 }
249
250 enum
251 {
252         H_CONTENT_TRANSFER_ENCODING = 0,
253         H_CONTENT_TYPE              = 1,
254         H_CONTENT_DISPOSITION       = 2,
255         H_CONTENT_DESCRIPTION       = 3,
256         H_SUBJECT                   = 4
257 };
258
259 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
260 {
261         const gchar *value;
262
263         g_return_val_if_fail(mimeinfo != NULL, NULL);
264         g_return_val_if_fail(name != NULL, NULL);
265
266         value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
267         if (value == NULL)
268                 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
269         
270         return value;
271 }
272
273 gboolean procmime_decode_content(MimeInfo *mimeinfo)
274 {
275         gchar buf[BUFFSIZE];
276         gint readend;
277         gchar *tmpfilename;
278         FILE *outfp, *infp;
279         struct stat statbuf;
280         gboolean tmp_file = FALSE;
281
282         EncodingType encoding = forced_encoding 
283                                 ? forced_encoding
284                                 : mimeinfo->encoding_type;
285                    
286         g_return_val_if_fail(mimeinfo != NULL, FALSE);
287
288         if (encoding == ENC_UNKNOWN ||
289             encoding == ENC_BINARY)
290                 return TRUE;
291
292         infp = fopen(mimeinfo->data.filename, "rb");
293         if (!infp) {
294                 perror("fopen");
295                 return FALSE;
296         }
297         fseek(infp, mimeinfo->offset, SEEK_SET);
298
299         outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
300         if (!outfp) {
301                 perror("tmpfile");
302                 return FALSE;
303         }
304         tmp_file = TRUE;
305         readend = mimeinfo->offset + mimeinfo->length;
306
307         if (encoding == ENC_QUOTED_PRINTABLE) {
308                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
309                         gint len;
310                         len = qp_decode_line(buf);
311                         fwrite(buf, len, 1, outfp);
312                 }
313         } else if (encoding == ENC_BASE64) {
314                 gchar outbuf[BUFFSIZE];
315                 gint len;
316                 Base64Decoder *decoder;
317                 gboolean uncanonicalize = FALSE;
318                 FILE *tmpfp = outfp;
319
320                 if (mimeinfo->type == MIMETYPE_TEXT ||
321                     mimeinfo->type == MIMETYPE_MESSAGE) {
322                         uncanonicalize = TRUE;
323                         tmpfp = my_tmpfile();
324                         if (!tmpfp) {
325                                 perror("tmpfile");
326                                 if (tmp_file) fclose(outfp);
327                                 return FALSE;
328                         }
329                 }
330
331                 decoder = base64_decoder_new();
332                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
333                         len = base64_decoder_decode(decoder, buf, outbuf);
334                         if (len < 0) {
335                                 g_warning("Bad BASE64 content\n");
336                                 break;
337                         }
338                         fwrite(outbuf, sizeof(gchar), len, tmpfp);
339                 }
340                 base64_decoder_free(decoder);
341
342                 if (uncanonicalize) {
343                         rewind(tmpfp);
344                         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
345                                 strcrchomp(buf);
346                                 fputs(buf, outfp);
347                         }
348                         fclose(tmpfp);
349                 }
350         } else if (encoding == ENC_X_UUENCODE) {
351                 gchar outbuf[BUFFSIZE];
352                 gint len;
353                 gboolean flag = FALSE;
354
355                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
356                         if (!flag && strncmp(buf,"begin ", 6)) continue;
357
358                         if (flag) {
359                                 len = fromuutobits(outbuf, buf);
360                                 if (len <= 0) {
361                                         if (len < 0) 
362                                                 g_warning("Bad UUENCODE content(%d)\n", len);
363                                         break;
364                                 }
365                                 fwrite(outbuf, sizeof(gchar), len, outfp);
366                         } else
367                                 flag = TRUE;
368                 }
369         } else {
370                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
371                         fputs(buf, outfp);
372                 }
373         }
374
375         fclose(outfp);
376         fclose(infp);
377
378         stat(tmpfilename, &statbuf);
379         if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
380                 unlink(mimeinfo->data.filename);
381         if (mimeinfo->data.filename != NULL)
382                 g_free(mimeinfo->data.filename);
383         mimeinfo->data.filename = tmpfilename;
384         mimeinfo->tmp = TRUE;
385         mimeinfo->offset = 0;
386         mimeinfo->length = statbuf.st_size;
387         mimeinfo->encoding_type = ENC_BINARY;
388
389         return TRUE;
390 }
391
392 #define B64_LINE_SIZE           57
393 #define B64_BUFFSIZE            77
394
395 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
396 {
397         FILE *infp, *outfp;
398         gint len;
399         gchar *tmpfilename;
400         struct stat statbuf;
401
402         if (mimeinfo->encoding_type != ENC_UNKNOWN &&
403             mimeinfo->encoding_type != ENC_BINARY &&
404             mimeinfo->encoding_type != ENC_7BIT &&
405             mimeinfo->encoding_type != ENC_8BIT)
406                 if(!procmime_decode_content(mimeinfo))
407                         return FALSE;
408
409         outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
410         if (!outfp) {
411                 perror("tmpfile");
412                 return FALSE;
413         }
414
415         if (mimeinfo->content == MIMECONTENT_FILE) {
416                 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
417                         g_warning("Can't open file %s\n", mimeinfo->data.filename);
418                         return FALSE;
419                 }
420         } else if (mimeinfo->content == MIMECONTENT_MEM) {
421                 infp = str_open_as_stream(mimeinfo->data.mem);
422                 if (infp == NULL)
423                         return FALSE;
424         }
425
426         if (encoding == ENC_BASE64) {
427                 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
428                 FILE *tmp_fp = infp;
429                 gchar *tmp_file = NULL;
430
431                 if (mimeinfo->type == MIMETYPE_TEXT ||
432                      mimeinfo->type == MIMETYPE_MESSAGE) {
433                         if (mimeinfo->content == MIMECONTENT_FILE) {
434                                 tmp_file = get_tmp_file();
435                                 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
436                                         g_free(tmp_file);
437                                         fclose(infp);
438                                         return FALSE;
439                                 }
440                                 if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
441                                         FILE_OP_ERROR(tmp_file, "fopen");
442                                         unlink(tmp_file);
443                                         g_free(tmp_file);
444                                         fclose(infp);
445                                         return FALSE;
446                                 }
447                         } else {
448                                 gchar *out = canonicalize_str(mimeinfo->data.mem);
449                                 fclose(infp);
450                                 infp = str_open_as_stream(out);
451                                 g_free(out);
452                                 if (infp == NULL)
453                                         return FALSE;
454                         }
455                 }
456
457                 while ((len = fread(inbuf, sizeof(gchar),
458                                     B64_LINE_SIZE, tmp_fp))
459                        == B64_LINE_SIZE) {
460                         base64_encode(outbuf, inbuf, B64_LINE_SIZE);
461                         fputs(outbuf, outfp);
462                         fputc('\n', outfp);
463                 }
464                 if (len > 0 && feof(tmp_fp)) {
465                         base64_encode(outbuf, inbuf, len);
466                         fputs(outbuf, outfp);
467                         fputc('\n', outfp);
468                 }
469
470                 if (tmp_file) {
471                         fclose(tmp_fp);
472                         unlink(tmp_file);
473                         g_free(tmp_file);
474                 }
475         } else if (encoding == ENC_QUOTED_PRINTABLE) {
476                 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
477
478                 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
479                         qp_encode_line(outbuf, inbuf);
480
481                         if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
482                                 gchar *tmpbuf = outbuf;
483                                 
484                                 tmpbuf += sizeof("From ")-1;
485                                 
486                                 fputs("=46rom ", outfp);
487                                 fputs(tmpbuf, outfp);
488                         } else 
489                                 fputs(outbuf, outfp);
490                 }
491         } else {
492                 gchar buf[BUFFSIZE];
493
494                 while (fgets(buf, sizeof(buf), infp) != NULL) {
495                         strcrchomp(buf);
496                         fputs(buf, outfp);
497                 }
498         }
499
500         fclose(outfp);
501         fclose(infp);
502
503         if (mimeinfo->content == MIMECONTENT_FILE) {
504                 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
505                         unlink(mimeinfo->data.filename);
506                 g_free(mimeinfo->data.filename);
507         } else if (mimeinfo->content == MIMECONTENT_MEM) {
508                 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
509                         g_free(mimeinfo->data.mem);
510         }
511
512         stat(tmpfilename, &statbuf);
513         mimeinfo->content = MIMECONTENT_FILE;
514         mimeinfo->data.filename = tmpfilename;
515         mimeinfo->tmp = TRUE;
516         mimeinfo->offset = 0;
517         mimeinfo->length = statbuf.st_size;
518         mimeinfo->encoding_type = encoding;
519
520         return TRUE;
521 }
522
523 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
524 {
525         FILE *infp, *outfp;
526         gchar buf[BUFFSIZE];
527         gint restlength, readlength;
528
529         g_return_val_if_fail(outfile != NULL, -1);
530         g_return_val_if_fail(mimeinfo != NULL, -1);
531
532         if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
533                 return -1;
534
535         if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
536                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
537                 return -1;
538         }
539         if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
540                 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
541                 fclose(infp);
542                 return -1;
543         }
544         if ((outfp = fopen(outfile, "wb")) == NULL) {
545                 FILE_OP_ERROR(outfile, "fopen");
546                 fclose(infp);
547                 return -1;
548         }
549
550         restlength = mimeinfo->length;
551
552         while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
553                 fwrite(buf, 1, readlength, outfp);
554                 restlength -= readlength;
555         }
556
557         fclose(infp);
558         if (fclose(outfp) == EOF) {
559                 FILE_OP_ERROR(outfile, "fclose");
560                 unlink(outfile);
561                 return -1;
562         }
563
564         return 0;
565 }
566
567 struct ContentRenderer {
568         char * content_type;
569         char * renderer;
570 };
571
572 static GList * renderer_list = NULL;
573
574 static struct ContentRenderer *
575 content_renderer_new(char * content_type, char * renderer)
576 {
577         struct ContentRenderer * cr;
578
579         cr = g_new(struct ContentRenderer, 1);
580         if (cr == NULL)
581                 return NULL;
582
583         cr->content_type = g_strdup(content_type);
584         cr->renderer = g_strdup(renderer);
585
586         return cr;
587 }
588
589 static void content_renderer_free(struct ContentRenderer * cr)
590 {
591         g_free(cr->content_type);
592         g_free(cr->renderer);
593         g_free(cr);
594 }
595
596 void renderer_read_config(void)
597 {
598         gchar buf[BUFFSIZE];
599         FILE * f;
600         gchar * rcpath;
601
602         g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
603         renderer_list = NULL;
604
605         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
606         f = fopen(rcpath, "rb");
607         g_free(rcpath);
608         
609         if (f == NULL)
610                 return;
611
612         while (fgets(buf, BUFFSIZE, f)) {
613                 char * p;
614                 struct ContentRenderer * cr;
615
616                 strretchomp(buf);
617                 p = strchr(buf, ' ');
618                 if (p == NULL)
619                         continue;
620                 * p = 0;
621
622                 cr = content_renderer_new(buf, p + 1);
623                 if (cr == NULL)
624                         continue;
625
626                 renderer_list = g_list_append(renderer_list, cr);
627         }
628
629         fclose(f);
630 }
631
632 void renderer_write_config(void)
633 {
634         gchar * rcpath;
635         PrefFile *pfile;
636         GList * cur;
637
638         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
639         
640         if ((pfile = prefs_write_open(rcpath)) == NULL) {
641                 g_warning("failed to write configuration to file\n");
642                 g_free(rcpath);
643                 return;
644         }
645
646         g_free(rcpath);
647
648         for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
649                 struct ContentRenderer * renderer;
650                 renderer = cur->data;
651                 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
652                         renderer->renderer);
653         }
654
655         if (prefs_file_close(pfile) < 0) {
656                 g_warning("failed to write configuration to file\n");
657                 return;
658         }
659 }
660
661 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
662 {
663         FILE *tmpfp, *outfp;
664         const gchar *src_codeset;
665         gboolean conv_fail = FALSE;
666         gchar buf[BUFFSIZE];
667         gchar *str;
668         struct ContentRenderer * renderer;
669         GList * cur;
670         gchar *tmpfile, *content_type;
671     
672         g_return_val_if_fail(mimeinfo != NULL, NULL);
673
674         if (!procmime_decode_content(mimeinfo))
675                 return NULL;
676
677         tmpfile = procmime_get_tmp_file_name(mimeinfo);
678         if (tmpfile == NULL)
679                 return NULL;
680
681         if (procmime_get_part(tmpfile, mimeinfo) < 0) {
682                 g_free(tmpfile);
683                 return NULL;
684         }
685
686         tmpfp = fopen(tmpfile, "rb");
687         if (tmpfp == NULL) {
688                 g_free(tmpfile);
689                 return NULL;
690         }
691
692         if ((outfp = my_tmpfile()) == NULL) {
693                 perror("tmpfile");
694                 fclose(tmpfp);
695                 g_free(tmpfile);
696                 return NULL;
697         }
698
699         src_codeset = forced_charset
700                       ? forced_charset : 
701                       procmime_mimeinfo_get_parameter(mimeinfo, "charset");
702
703         renderer = NULL;
704
705         content_type = procmime_get_content_type_str(mimeinfo->type,
706                                                      mimeinfo->subtype);
707         for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
708                 struct ContentRenderer * cr;
709
710                 cr = cur->data;
711                 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
712                         renderer = cr;
713                         break;
714                 }
715         }
716         g_free(content_type);
717
718         if (renderer != NULL) {
719                 FILE * p;
720                 int oldout;
721                 
722                 oldout = dup(1);
723                 
724                 dup2(fileno(outfp), 1);
725                 
726                 p = popen(renderer->renderer, "w");
727                 if (p != NULL) {
728                         size_t count;
729                         
730                         while ((count =
731                                 fread(buf, sizeof(char), sizeof(buf),
732                                       tmpfp)) > 0)
733                                 fwrite(buf, sizeof(char), count, p);
734                         pclose(p);
735                 }
736                 
737                 dup2(oldout, 1);
738 #warning FIXME_GTK2 HTML/RTF not yet utf8
739 /* CodeConverter seems to have no effect here */
740         } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
741                 HTMLParser *parser;
742                 CodeConverter *conv;
743
744                 conv = conv_code_converter_new(src_codeset);
745                 parser = html_parser_new(tmpfp, conv);
746                 while ((str = html_parse(parser)) != NULL) {
747                         fputs(str, outfp);
748                 }
749                 html_parser_destroy(parser);
750                 conv_code_converter_destroy(conv);
751         } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
752                 ERTFParser *parser;
753                 CodeConverter *conv;
754
755                 conv = conv_code_converter_new(src_codeset);
756                 parser = ertf_parser_new(tmpfp, conv);
757                 while ((str = ertf_parse(parser)) != NULL) {
758                         fputs(str, outfp);
759                 }
760                 ertf_parser_destroy(parser);
761                 conv_code_converter_destroy(conv);
762         } else if (mimeinfo->type == MIMETYPE_TEXT) {
763                 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
764                         str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
765                         if (str) {
766                                 fputs(str, outfp);
767                                 g_free(str);
768                         } else {
769                                 conv_fail = TRUE;
770                                 fputs(buf, outfp);
771                         }
772                 }
773         }
774
775         if (conv_fail)
776                 g_warning("procmime_get_text_content(): Code conversion failed.\n");
777
778         fclose(tmpfp);
779         rewind(outfp);
780         unlink(tmpfile);
781         g_free(tmpfile);
782
783         return outfp;
784 }
785
786 /* search the first text part of (multipart) MIME message,
787    decode, convert it and output to outfp. */
788 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
789 {
790         FILE *outfp = NULL;
791         MimeInfo *mimeinfo, *partinfo;
792
793         g_return_val_if_fail(msginfo != NULL, NULL);
794
795         mimeinfo = procmime_scan_message(msginfo);
796         if (!mimeinfo) return NULL;
797
798         partinfo = mimeinfo;
799         while (partinfo && partinfo->type != MIMETYPE_TEXT)
800                 partinfo = procmime_mimeinfo_next(partinfo);
801
802         if (partinfo)
803                 outfp = procmime_get_text_content(partinfo);
804
805         procmime_mimeinfo_free_all(mimeinfo);
806
807         return outfp;
808 }
809
810 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
811                                    const gchar *str, StrFindFunc find_func)
812 {
813         FILE *outfp;
814         gchar buf[BUFFSIZE];
815
816         g_return_val_if_fail(mimeinfo != NULL, FALSE);
817         g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
818         g_return_val_if_fail(str != NULL, FALSE);
819         g_return_val_if_fail(find_func != NULL, FALSE);
820
821         outfp = procmime_get_text_content(mimeinfo);
822
823         if (!outfp)
824                 return FALSE;
825
826         while (fgets(buf, sizeof(buf), outfp) != NULL) {
827                 strretchomp(buf);
828                 if (find_func(buf, str)) {
829                         fclose(outfp);
830                         return TRUE;
831                 }
832         }
833
834         fclose(outfp);
835
836         return FALSE;
837 }
838
839 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
840                               StrFindFunc find_func)
841 {
842         MimeInfo *mimeinfo;
843         MimeInfo *partinfo;
844         gchar *filename;
845         gboolean found = FALSE;
846
847         g_return_val_if_fail(msginfo != NULL, FALSE);
848         g_return_val_if_fail(str != NULL, FALSE);
849         g_return_val_if_fail(find_func != NULL, FALSE);
850
851         filename = procmsg_get_message_file(msginfo);
852         if (!filename) return FALSE;
853         mimeinfo = procmime_scan_message(msginfo);
854
855         for (partinfo = mimeinfo; partinfo != NULL;
856              partinfo = procmime_mimeinfo_next(partinfo)) {
857                 if (partinfo->type == MIMETYPE_TEXT) {
858                         if (procmime_find_string_part
859                                 (partinfo, filename, str, find_func) == TRUE) {
860                                 found = TRUE;
861                                 break;
862                         }
863                 }
864         }
865
866         procmime_mimeinfo_free_all(mimeinfo);
867         g_free(filename);
868
869         return found;
870 }
871
872 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
873 {
874         static guint32 id = 0;
875         gchar *base;
876         gchar *filename;
877         gchar f_prefix[10];
878
879         g_return_val_if_fail(mimeinfo != NULL, NULL);
880
881         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
882
883         if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
884                 base = g_strdup("mimetmp.html");
885         else {
886                 const gchar *basetmp;
887
888                 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
889                 if (basetmp == NULL)
890                         basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
891                 if (basetmp == NULL)
892                         basetmp = "mimetmp";
893                 basetmp = g_path_get_basename(basetmp);
894                 if (*basetmp == '\0') basetmp = g_strdup("mimetmp");
895                 base = conv_filename_from_utf8(basetmp);
896                 subst_for_shellsafe_filename(base);
897         }
898
899         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
900                                f_prefix, base, NULL);
901
902         g_free(base);
903         
904         return filename;
905 }
906
907 static GList *mime_type_list = NULL;
908
909 gchar *procmime_get_mime_type(const gchar *filename)
910 {
911         static GHashTable *mime_type_table = NULL;
912         MimeType *mime_type;
913         const gchar *p;
914         gchar *ext;
915         gchar *base;
916
917         if (!mime_type_table) {
918                 mime_type_table = procmime_get_mime_type_table();
919                 if (!mime_type_table) return NULL;
920         }
921
922         base = g_path_get_basename(filename);
923         p = strrchr(base, '.');
924         g_free(base);
925         if (!p) return NULL;
926
927         Xstrdup_a(ext, p + 1, return NULL);
928         g_strdown(ext);
929         mime_type = g_hash_table_lookup(mime_type_table, ext);
930         if (mime_type) {
931                 gchar *str;
932
933                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
934                                   NULL);
935                 return str;
936         }
937
938         return NULL;
939 }
940
941 static guint procmime_str_hash(gconstpointer gptr)
942 {
943         guint hash_result = 0;
944         const char *str;
945
946         for (str = gptr; str && *str; str++) {
947                 if (isupper(*str)) hash_result += (*str + ' ');
948                 else hash_result += *str;
949         }
950
951         return hash_result;
952 }
953
954 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
955 {
956         const char *str1 = gptr1;
957         const char *str2 = gptr2;
958
959         return !g_utf8_collate(str1, str2);
960 }
961
962 static GHashTable *procmime_get_mime_type_table(void)
963 {
964         GHashTable *table = NULL;
965         GList *cur;
966         MimeType *mime_type;
967         gchar **exts;
968
969         if (!mime_type_list) {
970                 mime_type_list = procmime_get_mime_type_list();
971                 if (!mime_type_list) return NULL;
972         }
973
974         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
975
976         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
977                 gint i;
978                 gchar *key;
979
980                 mime_type = (MimeType *)cur->data;
981
982                 if (!mime_type->extension) continue;
983
984                 exts = g_strsplit(mime_type->extension, " ", 16);
985                 for (i = 0; exts[i] != NULL; i++) {
986                         /* make the key case insensitive */
987                         g_strdown(exts[i]);
988                         /* use previously dup'd key on overwriting */
989                         if (g_hash_table_lookup(table, exts[i]))
990                                 key = exts[i];
991                         else
992                                 key = g_strdup(exts[i]);
993                         g_hash_table_insert(table, key, mime_type);
994                 }
995                 g_strfreev(exts);
996         }
997
998         return table;
999 }
1000
1001 GList *procmime_get_mime_type_list(void)
1002 {
1003         GList *list = NULL;
1004         FILE *fp;
1005         gchar buf[BUFFSIZE];
1006         guchar *p;
1007         gchar *delim;
1008         MimeType *mime_type;
1009
1010         if (mime_type_list) 
1011                 return mime_type_list;
1012
1013         if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1014                 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1015                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1016                         return NULL;
1017                 }
1018         }
1019
1020         while (fgets(buf, sizeof(buf), fp) != NULL) {
1021                 p = strchr(buf, '#');
1022                 if (p) *p = '\0';
1023                 g_strstrip(buf);
1024
1025                 p = buf;
1026                 while (*p && !isspace(*p)) p++;
1027                 if (*p) {
1028                         *p = '\0';
1029                         p++;
1030                 }
1031                 delim = strchr(buf, '/');
1032                 if (delim == NULL) continue;
1033                 *delim = '\0';
1034
1035                 mime_type = g_new(MimeType, 1);
1036                 mime_type->type = g_strdup(buf);
1037                 mime_type->sub_type = g_strdup(delim + 1);
1038
1039                 while (*p && isspace(*p)) p++;
1040                 if (*p)
1041                         mime_type->extension = g_strdup(p);
1042                 else
1043                         mime_type->extension = NULL;
1044
1045                 list = g_list_append(list, mime_type);
1046         }
1047
1048         fclose(fp);
1049
1050         if (!list)
1051                 g_warning("Can't read mime.types\n");
1052
1053         return list;
1054 }
1055
1056 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1057 {
1058         if (!charset)
1059                 return ENC_8BIT;
1060         else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1061                  !g_ascii_strcasecmp(charset, "US-ASCII"))
1062                 return ENC_7BIT;
1063         else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1064                  !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1065                  !g_ascii_strcasecmp(charset, "Windows-1251"))
1066                 return ENC_8BIT;
1067         else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1068                 return ENC_QUOTED_PRINTABLE;
1069         else
1070                 return ENC_8BIT;
1071 }
1072
1073 EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1074 {
1075         FILE *fp;
1076         guchar buf[BUFFSIZE];
1077         size_t len;
1078         size_t octet_chars = 0;
1079         size_t total_len = 0;
1080         gfloat octet_percentage;
1081
1082         if ((fp = fopen(file, "rb")) == NULL) {
1083                 FILE_OP_ERROR(file, "fopen");
1084                 return ENC_UNKNOWN;
1085         }
1086
1087         while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1088                 guchar *p;
1089                 gint i;
1090
1091                 for (p = buf, i = 0; i < len; ++p, ++i) {
1092                         if (*p & 0x80)
1093                                 ++octet_chars;
1094                 }
1095                 total_len += len;
1096         }
1097
1098         fclose(fp);
1099         
1100         if (total_len > 0)
1101                 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1102         else
1103                 octet_percentage = 0.0;
1104
1105         debug_print("procmime_get_encoding_for_text_file(): "
1106                     "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1107                     100.0 * octet_percentage);
1108
1109         if (octet_percentage > 0.20) {
1110                 debug_print("using BASE64\n");
1111                 return ENC_BASE64;
1112         } else if (octet_chars > 0) {
1113                 debug_print("using quoted-printable\n");
1114                 return ENC_QUOTED_PRINTABLE;
1115         } else {
1116                 debug_print("using 7bit\n");
1117                 return ENC_7BIT;
1118         }
1119 }
1120
1121 struct EncodingTable 
1122 {
1123         gchar *str;
1124         EncodingType enc_type;
1125 };
1126
1127 struct EncodingTable encoding_table[] = {
1128         {"7bit", ENC_7BIT},
1129         {"8bit", ENC_8BIT},
1130         {"binary", ENC_BINARY},
1131         {"quoted-printable", ENC_QUOTED_PRINTABLE},
1132         {"base64", ENC_BASE64},
1133         {"x-uuencode", ENC_UNKNOWN},
1134         {NULL, ENC_UNKNOWN},
1135 };
1136
1137 const gchar *procmime_get_encoding_str(EncodingType encoding)
1138 {
1139         struct EncodingTable *enc_table;
1140         
1141         for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1142                 if (enc_table->enc_type == encoding)
1143                         return enc_table->str;
1144         }
1145         return NULL;
1146 }
1147
1148 /* --- NEW MIME STUFF --- */
1149 struct TypeTable
1150 {
1151         gchar *str;
1152         MimeMediaType type;
1153 };
1154
1155 static struct TypeTable mime_type_table[] = {
1156         {"text", MIMETYPE_TEXT},
1157         {"image", MIMETYPE_IMAGE},
1158         {"audio", MIMETYPE_AUDIO},
1159         {"video", MIMETYPE_VIDEO},
1160         {"application", MIMETYPE_APPLICATION},
1161         {"message", MIMETYPE_MESSAGE},
1162         {"multipart", MIMETYPE_MULTIPART},
1163         {NULL, 0},
1164 };
1165
1166 const gchar *procmime_get_media_type_str(MimeMediaType type)
1167 {
1168         struct TypeTable *type_table;
1169         
1170         for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1171                 if (type_table->type == type)
1172                         return type_table->str;
1173         }
1174         return NULL;
1175 }
1176
1177 MimeMediaType procmime_get_media_type(const gchar *str)
1178 {
1179         struct TypeTable *typetablearray;
1180
1181         for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1182                 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1183                         return typetablearray->type;
1184
1185         return MIMETYPE_UNKNOWN;
1186 }
1187
1188 /*!
1189  *\brief        Safe wrapper for content type string.
1190  *
1191  *\return       const gchar * Pointer to content type string. 
1192  */
1193 gchar *procmime_get_content_type_str(MimeMediaType type,
1194                                            const char *subtype)
1195 {
1196         const gchar *type_str = NULL;
1197
1198         if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1199                 return g_strdup("unknown");
1200         return g_strdup_printf("%s/%s", type_str, subtype);
1201 }
1202
1203 void procmime_parse_mimepart(MimeInfo *parent,
1204                              gchar *content_type,
1205                              gchar *content_encoding,
1206                              gchar *content_description,
1207                              gchar *content_id,
1208                              gchar *content_disposition,
1209                              const gchar *filename,
1210                              guint offset,
1211                              guint length);
1212
1213 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1214 {
1215         HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
1216                                 {"Content-Transfer-Encoding:",
1217                                                    NULL, FALSE},
1218                                 {"Content-Description:",
1219                                                    NULL, TRUE},
1220                                 {"Content-ID:",
1221                                                    NULL, TRUE},
1222                                 {"Content-Disposition:",
1223                                                    NULL, TRUE},
1224                                 {"MIME-Version:",
1225                                                    NULL, TRUE},
1226                                 {NULL,             NULL, FALSE}};
1227         guint content_start, i;
1228         FILE *fp;
1229         gint mime_major, mime_minor;
1230         gchar *tmp;
1231
1232         procmime_decode_content(mimeinfo);
1233
1234         fp = fopen(mimeinfo->data.filename, "rb");
1235         if (fp == NULL) {
1236                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1237                 return;
1238         }
1239         fseek(fp, mimeinfo->offset, SEEK_SET);
1240         procheader_get_header_fields(fp, hentry);
1241         if (hentry[0].body != NULL) {
1242                 tmp = conv_unmime_header(hentry[0].body, NULL);
1243                 g_free(hentry[0].body);
1244                 hentry[0].body = tmp;
1245         }                
1246         if (hentry[2].body != NULL) {
1247                 tmp = conv_unmime_header(hentry[2].body, NULL);
1248                 g_free(hentry[2].body);
1249                 hentry[2].body = tmp;
1250         }                
1251         if (hentry[4].body != NULL) {
1252                 tmp = conv_unmime_header(hentry[4].body, NULL);
1253                 g_free(hentry[4].body);
1254                 hentry[4].body = tmp;
1255         }                
1256         content_start = ftell(fp);
1257         fclose(fp);
1258
1259         if ((hentry[5].body != NULL) &&
1260             (sscanf(hentry[5].body, "%d.%d", &mime_major, &mime_minor) == 2) &&
1261             (mime_major == 1) && (mime_minor == 0)) {
1262                 procmime_parse_mimepart(mimeinfo,
1263                                         hentry[0].body, hentry[1].body,
1264                                         hentry[2].body, hentry[3].body, 
1265                                         hentry[4].body, 
1266                                         mimeinfo->data.filename, content_start,
1267                                         mimeinfo->length - (content_start - mimeinfo->offset));
1268         } else {
1269                 MimeInfo *subinfo;
1270
1271                 subinfo = procmime_mimeinfo_new();
1272                 subinfo->content = MIMECONTENT_FILE;
1273                 subinfo->encoding_type = ENC_UNKNOWN;
1274                 subinfo->type = MIMETYPE_TEXT;
1275                 subinfo->subtype = g_strdup("plain");
1276                 subinfo->data.filename = g_strdup(mimeinfo->data.filename);
1277                 subinfo->offset = content_start;
1278                 subinfo->length = mimeinfo->length - (content_start - mimeinfo->offset);
1279
1280                 g_node_append(mimeinfo->node, subinfo->node);
1281         }
1282         for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1283                 g_free(hentry[i].body);
1284                 hentry[i].body = NULL;
1285         }
1286 }
1287
1288 void procmime_parse_multipart(MimeInfo *mimeinfo)
1289 {
1290         HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
1291                                 {"Content-Transfer-Encoding:",
1292                                                    NULL, FALSE},
1293                                 {"Content-Description:",
1294                                                    NULL, TRUE},
1295                                 {"Content-ID:",
1296                                                    NULL, TRUE},
1297                                 {"Content-Disposition:",
1298                                                    NULL, TRUE},
1299                                 {NULL,             NULL, FALSE}};
1300         gchar *p, *tmp;
1301         gchar *boundary;
1302         gint boundary_len = 0, lastoffset = -1, i;
1303         gchar buf[BUFFSIZE];
1304         FILE *fp;
1305
1306         boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1307         if (!boundary)
1308                 return;
1309         boundary_len = strlen(boundary);
1310
1311         procmime_decode_content(mimeinfo);
1312
1313         fp = fopen(mimeinfo->data.filename, "rb");
1314         if (fp == NULL) {
1315                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1316                 return;
1317         }
1318         fseek(fp, mimeinfo->offset, SEEK_SET);
1319         while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1320                 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1321                         break;
1322
1323                 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1324                         if (lastoffset != -1) {
1325                                 procmime_parse_mimepart(mimeinfo,
1326                                                         hentry[0].body, hentry[1].body,
1327                                                         hentry[2].body, hentry[3].body, 
1328                                                         hentry[4].body, 
1329                                                         mimeinfo->data.filename, lastoffset,
1330                                                         (ftell(fp) - strlen(buf)) - lastoffset - 1);
1331                         }
1332                         
1333                         if (buf[2 + boundary_len]     == '-' &&
1334                             buf[2 + boundary_len + 1] == '-')
1335                                 break;
1336
1337                         for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1338                                 g_free(hentry[i].body);
1339                                 hentry[i].body = NULL;
1340                         }
1341                         procheader_get_header_fields(fp, hentry);
1342                         if (hentry[0].body != NULL) {
1343                                 tmp = conv_unmime_header(hentry[0].body, NULL);
1344                                 g_free(hentry[0].body);
1345                                 hentry[0].body = tmp;
1346                         }                
1347                         if (hentry[2].body != NULL) {
1348                                 tmp = conv_unmime_header(hentry[2].body, NULL);
1349                                 g_free(hentry[2].body);
1350                                 hentry[2].body = tmp;
1351                         }                
1352                         if (hentry[4].body != NULL) {
1353                                 tmp = conv_unmime_header(hentry[4].body, NULL);
1354                                 g_free(hentry[4].body);
1355                                 hentry[4].body = tmp;
1356                         }                
1357                         lastoffset = ftell(fp);
1358                 }
1359         }
1360         for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1361                 g_free(hentry[i].body);
1362                 hentry[i].body = NULL;
1363         }
1364         fclose(fp);
1365 }
1366
1367 static void parse_parameters(const gchar *parameters, GHashTable *table)
1368 {
1369         gchar *params, *param, *next;
1370         GSList *convlist = NULL, *concatlist = NULL, *cur;
1371
1372         params = g_strdup(parameters);
1373         param = params;
1374         next = params;
1375         for (; next != NULL; param = next) {
1376                 gchar *attribute, *value, *tmp;
1377                 gint len;
1378                 gboolean convert = FALSE;
1379
1380                 next = strchr_with_skip_quote(param, '"', ';');
1381                 if (next != NULL) {
1382                         next[0] = '\0';
1383                         next++;
1384                 }
1385
1386                 g_strstrip(param);
1387
1388                 attribute = param;
1389                 value = strchr(attribute, '=');
1390                 if (value == NULL)
1391                         continue;
1392
1393                 value[0] = '\0';
1394                 value++;
1395
1396                 g_strdown(attribute);
1397
1398                 len = strlen(attribute);
1399                 if (attribute[len - 1] == '*') {
1400                         gchar *srcpos, *dstpos, *endpos;
1401
1402                         convert = TRUE;
1403                         attribute[len - 1] = '\0';
1404
1405                         srcpos = value;
1406                         dstpos = value;
1407                         endpos = value + strlen(value);
1408                         while (srcpos < endpos) {
1409                                 if (*srcpos != '%')
1410                                         *dstpos = *srcpos;
1411                                 else {
1412                                         guchar dstvalue;
1413
1414                                         if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1415                                                 *dstpos = '?';
1416                                         else
1417                                                 *dstpos = dstvalue;
1418                                         srcpos += 2;
1419                                 }
1420                                 srcpos++;
1421                                 dstpos++;
1422                         }
1423                         *dstpos = '\0';
1424                 } else {
1425                         if (value[0] == '"')
1426                                 extract_quote(value, '"');
1427                         else if ((tmp = strchr(value, ' ')) != NULL)
1428                                 *tmp = '\0';
1429                 }
1430
1431                 if (strrchr(attribute, '*') != NULL) {
1432                         gchar *tmpattr;
1433
1434                         tmpattr = g_strdup(attribute);
1435                         tmp = strrchr(tmpattr, '*');
1436                         tmp[0] = '\0';
1437
1438                         if ((tmp[1] == '0') && (tmp[2] == '\0') && 
1439                             (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1440                                 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1441
1442                         if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1443                                 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1444
1445                         g_free(tmpattr);
1446                 } else if (convert) {
1447                         if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1448                                 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1449                 }
1450
1451                 if (g_hash_table_lookup(table, attribute) == NULL)
1452                         g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1453         }
1454
1455         for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1456                 gchar *attribute, *attrwnum, *partvalue;
1457                 gint n = 0;
1458                 GString *value;
1459
1460                 attribute = (gchar *) cur->data;
1461                 value = g_string_sized_new(64);
1462
1463                 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1464                 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1465                         g_string_append(value, partvalue);
1466
1467                         g_free(attrwnum);
1468                         n++;
1469                         attrwnum = g_strdup_printf("%s*%d", attribute, n);
1470                 }
1471                 g_free(attrwnum);
1472
1473                 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1474                 g_string_free(value, TRUE);
1475         }
1476         slist_free_strings(concatlist);
1477         g_slist_free(concatlist);
1478
1479         for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1480                 gchar *attribute, *key, *value;
1481                 gchar *charset, *lang, *oldvalue, *newvalue;
1482
1483                 attribute = (gchar *) cur->data;
1484                 if (!g_hash_table_lookup_extended(table, attribute, (gpointer *) &key, (gpointer *) &value))
1485                         continue;
1486
1487                 charset = value;
1488                 lang = strchr(charset, '\'');
1489                 if (lang == NULL)
1490                         continue;
1491                 lang[0] = '\0';
1492                 lang++;
1493                 oldvalue = strchr(lang, '\'');
1494                 if (oldvalue == NULL)
1495                         continue;
1496                 oldvalue[0] = '\0';
1497                 oldvalue++;
1498
1499                 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1500
1501                 g_hash_table_remove(table, attribute);
1502                 g_free(key);
1503                 g_free(value);
1504
1505                 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1506         }
1507         slist_free_strings(convlist);
1508         g_slist_free(convlist);
1509
1510         g_free(params);
1511 }       
1512
1513 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1514 {
1515         g_return_if_fail(content_type != NULL);
1516         g_return_if_fail(mimeinfo != NULL);
1517
1518         /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1519          * if it's not available we use the default Content-Type */
1520         if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1521                 mimeinfo->type = MIMETYPE_TEXT;
1522                 mimeinfo->subtype = g_strdup("plain");
1523                 if (g_hash_table_lookup(mimeinfo->typeparameters,
1524                                        "charset") == NULL)
1525                         g_hash_table_insert(mimeinfo->typeparameters,
1526                                             g_strdup("charset"),
1527                                             g_strdup("us-ascii"));
1528         } else {
1529                 gchar *type, *subtype, *params;
1530
1531                 type = g_strdup(content_type);
1532                 subtype = strchr(type, '/') + 1;
1533                 *(subtype - 1) = '\0';
1534                 if ((params = strchr(subtype, ';')) != NULL) {
1535                         params[0] = '\0';
1536                         params++;
1537                 }
1538
1539                 mimeinfo->type = procmime_get_media_type(type);
1540                 mimeinfo->subtype = g_strdup(subtype);
1541
1542                 /* Get mimeinfo->typeparameters */
1543                 if (params != NULL)
1544                         parse_parameters(params, mimeinfo->typeparameters);
1545
1546                 g_free(type);
1547         }
1548 }
1549
1550 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1551 {
1552         gchar *tmp, *params;
1553
1554         g_return_if_fail(content_disposition != NULL);
1555         g_return_if_fail(mimeinfo != NULL);
1556
1557         tmp = g_strdup(content_disposition);
1558         if ((params = strchr(tmp, ';')) != NULL) {
1559                 params[0] = '\0';
1560                 params++;
1561         }       
1562         g_strstrip(tmp);
1563
1564         if (!g_ascii_strcasecmp(tmp, "inline")) 
1565                 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1566         else if (!g_ascii_strcasecmp(tmp, "attachment"))
1567                 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1568         else
1569                 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1570         
1571         if (params != NULL)
1572                 parse_parameters(params, mimeinfo->dispositionparameters);
1573
1574         g_free(tmp);
1575 }
1576
1577
1578 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1579 {
1580         struct EncodingTable *enc_table;
1581         
1582         for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1583                 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1584                         mimeinfo->encoding_type = enc_table->enc_type;
1585                         return;
1586                 }
1587         }
1588         mimeinfo->encoding_type = ENC_UNKNOWN;
1589         return;
1590 }
1591
1592 void procmime_parse_mimepart(MimeInfo *parent,
1593                              gchar *content_type,
1594                              gchar *content_encoding,
1595                              gchar *content_description,
1596                              gchar *content_id,
1597                              gchar *content_disposition,
1598                              const gchar *filename,
1599                              guint offset,
1600                              guint length)
1601 {
1602         MimeInfo *mimeinfo;
1603
1604         /* Create MimeInfo */
1605         mimeinfo = procmime_mimeinfo_new();
1606         mimeinfo->content = MIMECONTENT_FILE;
1607         if (parent != NULL)
1608                 g_node_append(parent->node, mimeinfo->node);
1609         mimeinfo->data.filename = g_strdup(filename);
1610         mimeinfo->offset = offset;
1611         mimeinfo->length = length;
1612
1613         if (content_type != NULL) {
1614                 procmime_parse_content_type(content_type, mimeinfo);
1615         } else {
1616                 mimeinfo->type = MIMETYPE_TEXT;
1617                 mimeinfo->subtype = g_strdup("plain");
1618                 if (g_hash_table_lookup(mimeinfo->typeparameters,
1619                                        "charset") == NULL)
1620                         g_hash_table_insert(mimeinfo->typeparameters, g_strdup("charset"), g_strdup("us-ascii"));
1621         }
1622
1623         if (content_encoding != NULL) {
1624                 procmime_parse_content_encoding(content_encoding, mimeinfo);
1625         } else {
1626                 mimeinfo->encoding_type = ENC_UNKNOWN;
1627         }
1628
1629         if (content_description != NULL)
1630                 mimeinfo->description = g_strdup(content_description);
1631         else
1632                 mimeinfo->description = NULL;
1633
1634         if (content_id != NULL)
1635                 mimeinfo->id = g_strdup(content_id);
1636         else
1637                 mimeinfo->id = NULL;
1638
1639         if (content_disposition != NULL) 
1640                 procmime_parse_content_disposition(content_disposition, mimeinfo);
1641         else
1642                 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1643
1644         /* Call parser for mime type */
1645         switch (mimeinfo->type) {
1646                 case MIMETYPE_MESSAGE:
1647                         if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1648                                 procmime_parse_message_rfc822(mimeinfo);
1649                         }
1650                         break;
1651                         
1652                 case MIMETYPE_MULTIPART:
1653                         procmime_parse_multipart(mimeinfo);
1654                         break;
1655                         
1656                 default:
1657                         break;
1658         }
1659 }
1660
1661 static gchar *typenames[] = {
1662     "text",
1663     "image",
1664     "audio",
1665     "video",
1666     "application",
1667     "message",
1668     "multipart",
1669     "unknown",
1670 };
1671
1672 static gboolean output_func(GNode *node, gpointer data)
1673 {
1674         guint i, depth;
1675         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1676
1677         depth = g_node_depth(node);
1678         for (i = 0; i < depth; i++)
1679                 printf("    ");
1680         printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1681
1682         return FALSE;
1683 }
1684
1685 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1686 {
1687         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1688 }
1689
1690 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1691 {
1692         MimeInfo *mimeinfo;
1693         struct stat buf;
1694
1695         stat(filename, &buf);
1696
1697         mimeinfo = procmime_mimeinfo_new();
1698         mimeinfo->content = MIMECONTENT_FILE;
1699         mimeinfo->encoding_type = ENC_UNKNOWN;
1700         mimeinfo->type = MIMETYPE_MESSAGE;
1701         mimeinfo->subtype = g_strdup("rfc822");
1702         mimeinfo->data.filename = g_strdup(filename);
1703         mimeinfo->offset = offset;
1704         mimeinfo->length = buf.st_size - offset;
1705
1706         procmime_parse_message_rfc822(mimeinfo);
1707         if (debug_get_mode())
1708                 output_mime_structure(mimeinfo, 0);
1709
1710         return mimeinfo;
1711 }
1712
1713 MimeInfo *procmime_scan_file(const gchar *filename)
1714 {
1715         MimeInfo *mimeinfo;
1716
1717         g_return_val_if_fail(filename != NULL, NULL);
1718
1719         mimeinfo = procmime_scan_file_with_offset(filename, 0);
1720
1721         return mimeinfo;
1722 }
1723
1724 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1725 {
1726         FILE *fp;
1727         MimeInfo *mimeinfo;
1728         gchar buf[BUFFSIZE];
1729         gint offset = 0;
1730
1731         g_return_val_if_fail(filename != NULL, NULL);
1732
1733         /* Open file */
1734         if ((fp = fopen(filename, "rb")) == NULL)
1735                 return NULL;
1736         /* Skip queue header */
1737         while (fgets(buf, sizeof(buf), fp) != NULL)
1738                 if (buf[0] == '\r' || buf[0] == '\n') break;
1739         offset = ftell(fp);
1740         fclose(fp);
1741
1742         mimeinfo = procmime_scan_file_with_offset(filename, offset);
1743
1744         return mimeinfo;
1745 }
1746
1747 typedef enum {
1748     ENC_AS_TOKEN,
1749     ENC_AS_QUOTED_STRING,
1750     ENC_AS_EXTENDED,
1751 } EncodeAs;
1752
1753 typedef struct _ParametersData {
1754         FILE *fp;
1755         guint len;
1756 } ParametersData;
1757
1758 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1759 {
1760         gchar *param = key;
1761         gchar *val = value, *valpos;
1762         ParametersData *pdata = (ParametersData *)user_data;
1763         GString *buf = g_string_new("");
1764
1765         EncodeAs encas = ENC_AS_TOKEN;
1766
1767         for (valpos = val; *valpos != 0; valpos++) {
1768                 if (!IS_ASCII(*valpos)) {
1769                         encas = ENC_AS_EXTENDED;
1770                         break;
1771                 }
1772             
1773                 /* CTLs */
1774                 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1775                         encas = ENC_AS_QUOTED_STRING;
1776                         continue;
1777                 }
1778
1779                 /* tspecials + SPACE */
1780                 switch (*valpos) {
1781                 case ' ':
1782                 case '(': 
1783                 case ')':
1784                 case '<':
1785                 case '>':
1786                 case '@':
1787                 case ',':
1788                 case ';':
1789                 case ':':
1790                 case '\\':
1791                 case '"':
1792                 case '/':
1793                 case '[':
1794                 case ']':
1795                 case '?':
1796                 case '=':
1797                         encas = ENC_AS_QUOTED_STRING;
1798                         continue;
1799                 }
1800         }
1801
1802         switch (encas) {
1803         case ENC_AS_TOKEN:
1804                 g_string_append_printf(buf, "%s=%s", param, val);
1805                 break;
1806
1807         case ENC_AS_QUOTED_STRING:
1808                 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1809                 break;
1810
1811         case ENC_AS_EXTENDED:
1812                 g_string_append_printf(buf, "%s*=%s''", param,
1813                         conv_get_locale_charset_str());
1814                 for (valpos = val; *valpos != '\0'; valpos++) {
1815                         if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1816                                 g_string_append_printf(buf, "%c", *valpos);
1817                         } else {
1818                                 gchar hexstr[3] = "XX";
1819                                 get_hex_str(hexstr, *valpos);
1820                                 g_string_append_printf(buf, "%%%s", hexstr);
1821                         }
1822                 }
1823                 break;
1824         }
1825         
1826         if (buf->str && strlen(buf->str)) {
1827                 if (pdata->len + strlen(buf->str) + 2 > 76) {
1828                         fprintf(pdata->fp, ";\n %s", buf->str);
1829                         pdata->len = strlen(buf->str) + 1;
1830                 } else {
1831                         fprintf(pdata->fp, "; %s", buf->str);
1832                         pdata->len += strlen(buf->str) + 2;
1833                 }
1834         }
1835         g_string_free(buf, TRUE);
1836 }
1837
1838 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1839 {
1840         struct TypeTable *type_table;
1841         ParametersData *pdata = g_new0(ParametersData, 1);
1842         debug_print("procmime_write_mime_header\n");
1843         
1844         pdata->fp = fp;
1845
1846         for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1847                 if (mimeinfo->type == type_table->type) {
1848                         gchar *buf = g_strdup_printf(
1849                                 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1850                         fprintf(fp, "%s", buf);
1851                         pdata->len = strlen(buf);
1852                         g_free(buf);
1853                         break;
1854                 }
1855         g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
1856         g_free(pdata);
1857
1858         fprintf(fp, "\n");
1859
1860         if (mimeinfo->encoding_type != ENC_UNKNOWN)
1861                 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
1862
1863         if (mimeinfo->description != NULL)
1864                 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
1865
1866         if (mimeinfo->id != NULL)
1867                 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
1868
1869         if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
1870                 ParametersData *pdata = g_new0(ParametersData, 1);
1871                 gchar *buf = NULL;
1872                 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
1873                         buf = g_strdup("Content-Disposition: inline");
1874                 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
1875                         buf = g_strdup("Content-Disposition: attachment");
1876                 else
1877                         buf = g_strdup("Content-Disposition: unknown");
1878
1879                 fprintf(fp, "%s", buf);
1880                 pdata->len = strlen(buf);
1881                 g_free(buf);
1882
1883                 pdata->fp = fp;
1884                 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
1885                 g_free(pdata);
1886                 fprintf(fp, "\n");
1887         }
1888
1889         fprintf(fp, "\n");
1890 }
1891
1892 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
1893 {
1894         FILE *infp;
1895         GNode *childnode;
1896         MimeInfo *child;
1897         gchar buf[BUFFSIZE];
1898         gboolean skip = FALSE;;
1899
1900         debug_print("procmime_write_message_rfc822\n");
1901
1902         /* write header */
1903         switch (mimeinfo->content) {
1904         case MIMECONTENT_FILE:
1905                 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1906                         FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1907                         return -1;
1908                 }
1909                 fseek(infp, mimeinfo->offset, SEEK_SET);
1910                 while (fgets(buf, sizeof(buf), infp) == buf) {
1911                         if (buf[0] == '\n' && buf[1] == '\0')
1912                                 break;
1913                         if (skip && (buf[0] == ' ' || buf[0] == '\t'))
1914                                 continue;
1915                         if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
1916                             g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
1917                             g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
1918                             g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
1919                             g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
1920                             g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
1921                                 skip = TRUE;
1922                                 continue;
1923                         }
1924                         fwrite(buf, sizeof(gchar), strlen(buf), fp);
1925                         skip = FALSE;
1926                 }
1927                 fclose(infp);
1928                 break;
1929
1930         case MIMECONTENT_MEM:
1931                 fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
1932                 break;
1933
1934         default:
1935                 break;
1936         }
1937
1938         childnode = mimeinfo->node->children;
1939         if (childnode == NULL)
1940                 return -1;
1941
1942         child = (MimeInfo *) childnode->data;
1943         fprintf(fp, "Mime-Version: 1.0\n");
1944         procmime_write_mime_header(child, fp);
1945         return procmime_write_mimeinfo(child, fp);
1946 }
1947
1948 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
1949 {
1950         FILE *infp;
1951         GNode *childnode;
1952         gchar *boundary, *str, *str2;
1953         gchar buf[BUFFSIZE];
1954         gboolean firstboundary;
1955
1956         debug_print("procmime_write_multipart\n");
1957
1958         boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1959
1960         switch (mimeinfo->content) {
1961         case MIMECONTENT_FILE:
1962                 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1963                         FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1964                         return -1;
1965                 }
1966                 fseek(infp, mimeinfo->offset, SEEK_SET);
1967                 while (fgets(buf, sizeof(buf), infp) == buf) {
1968                         if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
1969                                 break;
1970                         fwrite(buf, sizeof(gchar), strlen(buf), fp);
1971                 }
1972                 fclose(infp);
1973                 break;
1974
1975         case MIMECONTENT_MEM:
1976                 str = g_strdup(mimeinfo->data.mem);
1977                 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
1978                     (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
1979                         *(str2 - 2) = '\0';
1980                 fwrite(str, strlen(str), sizeof(gchar), fp);
1981                 g_free(str);
1982                 break;
1983
1984         default:
1985                 break;
1986         }
1987
1988         childnode = mimeinfo->node->children;
1989         firstboundary = TRUE;
1990         while (childnode != NULL) {
1991                 MimeInfo *child = childnode->data;
1992
1993                 if (firstboundary)
1994                         firstboundary = FALSE;
1995                 else
1996                         fprintf(fp, "\n");
1997                 fprintf(fp, "--%s\n", boundary);
1998
1999                 procmime_write_mime_header(child, fp);
2000                 if (procmime_write_mimeinfo(child, fp) < 0)
2001                         return -1;
2002
2003                 childnode = g_node_next_sibling(childnode);
2004         }       
2005         fprintf(fp, "\n--%s--\n", boundary);
2006
2007         return 0;
2008 }
2009
2010 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
2011 {
2012         FILE *infp;
2013
2014         debug_print("procmime_write_mimeinfo\n");
2015
2016         if (G_NODE_IS_LEAF(mimeinfo->node)) {
2017                 switch (mimeinfo->content) {
2018                 case MIMECONTENT_FILE:
2019                         if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
2020                                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
2021                                 return -1;
2022                         }
2023                         copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
2024                         fclose(infp);
2025                         return 0;
2026
2027                 case MIMECONTENT_MEM:
2028                         fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
2029                         return 0;
2030
2031                 default:
2032                         return 0;
2033                 }
2034         } else {
2035                 /* Call writer for mime type */
2036                 switch (mimeinfo->type) {
2037                 case MIMETYPE_MESSAGE:
2038                         if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0)
2039                                 return procmime_write_message_rfc822(mimeinfo, fp);
2040                         break;
2041                         
2042                 case MIMETYPE_MULTIPART:
2043                         return procmime_write_multipart(mimeinfo, fp);
2044                         
2045                 default:
2046                         break;
2047                 }
2048
2049                 return -1;
2050         }
2051
2052         return 0;
2053 }