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