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