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