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