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