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