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