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