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