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