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