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