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