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