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