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