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