2005-02-10 [paul] 1.0.1cvs3.2
[claws.git] / src / procmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws 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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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 #include <locale.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "procmime.h"
38 #include "procheader.h"
39 #include "base64.h"
40 #include "quoted-printable.h"
41 #include "uuencode.h"
42 #include "unmime.h"
43 #include "html.h"
44 #include "enriched.h"
45 #include "codeconv.h"
46 #include "utils.h"
47 #include "prefs_common.h"
48 #include "prefs_gtk.h"
49
50 static GHashTable *procmime_get_mime_type_table (void);
51
52 MimeInfo *procmime_mimeinfo_new(void)
53 {
54         MimeInfo *mimeinfo;
55
56         mimeinfo = g_new0(MimeInfo, 1);
57         mimeinfo->content        = MIMECONTENT_EMPTY;
58         mimeinfo->data.filename  = NULL;
59
60         mimeinfo->type           = MIMETYPE_UNKNOWN;
61         mimeinfo->encoding_type  = ENC_UNKNOWN;
62         mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
63
64         mimeinfo->disposition    = DISPOSITIONTYPE_UNKNOWN;
65         mimeinfo->dispositionparameters 
66                                  = g_hash_table_new(g_str_hash, g_str_equal);
67
68         mimeinfo->node           = g_node_new(mimeinfo);
69         
70         return mimeinfo;
71 }
72
73 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
74 {
75         g_free(key);
76         g_free(value);
77         
78         return TRUE;
79 }
80
81 static gchar *forced_charset = NULL;
82
83 void procmime_force_charset(const gchar *str)
84 {
85         g_free(forced_charset);
86         forced_charset = NULL;
87         if (str)
88                 forced_charset = g_strdup(str);
89 }
90
91 static EncodingType forced_encoding = 0;
92
93 void procmime_force_encoding(EncodingType encoding)
94 {
95         forced_encoding = encoding;
96 }
97
98 static gboolean free_func(GNode *node, gpointer data)
99 {
100         MimeInfo *mimeinfo = (MimeInfo *) node->data;
101
102         switch (mimeinfo->content) {
103         case MIMECONTENT_FILE:
104                 if (mimeinfo->tmp)
105                         unlink(mimeinfo->data.filename);
106                 g_free(mimeinfo->data.filename);
107                 break;
108
109         case MIMECONTENT_MEM:
110                 if (mimeinfo->tmp)
111                         g_free(mimeinfo->data.mem);
112         default:
113                 break;
114         }
115
116         g_free(mimeinfo->subtype);
117         g_free(mimeinfo->description);
118         g_free(mimeinfo->id);
119
120         g_hash_table_foreach_remove(mimeinfo->typeparameters,
121                 procmime_mimeinfo_parameters_destroy, NULL);
122         g_hash_table_destroy(mimeinfo->typeparameters);
123         g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
124                 procmime_mimeinfo_parameters_destroy, NULL);
125         g_hash_table_destroy(mimeinfo->dispositionparameters);
126
127         if (mimeinfo->privacy)
128                 privacy_free_privacydata(mimeinfo->privacy);
129
130         g_free(mimeinfo);
131
132         return FALSE;
133 }
134
135 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
136 {
137         GNode *node;
138
139         if (!mimeinfo)
140                 return;
141
142         node = mimeinfo->node;
143         g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
144
145         g_node_destroy(node);
146 }
147
148 #if 0 /* UNUSED */
149 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
150 {
151         MimeInfo *child = parent->children;
152
153         if (!child)
154                 parent->children = mimeinfo;
155         else {
156                 while (child->next != NULL)
157                         child = child->next;
158
159                 child->next = mimeinfo;
160         }
161
162         mimeinfo->parent = parent;
163         mimeinfo->level = parent->level + 1;
164
165         return mimeinfo;
166 }
167
168 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
169 {
170         MimeInfo *parent = old->parent;
171         MimeInfo *child;
172
173         g_return_if_fail(parent != NULL);
174         g_return_if_fail(new->next == NULL);
175
176         for (child = parent->children; child && child != old;
177              child = child->next)
178                 ;
179         if (!child) {
180                 g_warning("oops: parent can't find it's own child");
181                 return;
182         }
183         procmime_mimeinfo_free_all(old);
184
185         if (child == parent->children) {
186                 new->next = parent->children->next;
187                 parent->children = new;
188         } else {
189                 new->next = child->next;
190                 child = new;
191         }
192 }
193 #endif
194
195 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
196 {
197         g_return_val_if_fail(mimeinfo != NULL, NULL);
198         g_return_val_if_fail(mimeinfo->node != NULL, NULL);
199
200         if (mimeinfo->node->parent == NULL)
201                 return NULL;
202         return (MimeInfo *) mimeinfo->node->parent->data;
203 }
204
205 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
206 {
207         g_return_val_if_fail(mimeinfo != NULL, NULL);
208         g_return_val_if_fail(mimeinfo->node != NULL, NULL);
209
210         if (mimeinfo->node->children)
211                 return (MimeInfo *) mimeinfo->node->children->data;
212         if (mimeinfo->node->next)
213                 return (MimeInfo *) mimeinfo->node->next->data;
214
215         if (mimeinfo->node->parent == NULL)
216                 return NULL;
217
218         while (mimeinfo->node->parent != NULL) {
219                 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
220                 if (mimeinfo->node->next)
221                         return (MimeInfo *) mimeinfo->node->next->data;
222         }
223
224         return NULL;
225 }
226
227 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
228 {
229         gchar *filename;
230         MimeInfo *mimeinfo;
231
232         filename = procmsg_get_message_file_path(msginfo);
233         if (!filename || !is_file_exist(filename)) {
234                 g_free(filename);
235                 filename = procmsg_get_message_file(msginfo);
236         }
237         if (!filename || !is_file_exist(filename)) 
238                 return NULL;
239
240         if (msginfo->folder->stype != F_QUEUE && 
241             msginfo->folder->stype != F_DRAFT)
242                 mimeinfo = procmime_scan_file(filename);
243         else
244                 mimeinfo = procmime_scan_queue_file(filename);
245         g_free(filename);
246
247         return mimeinfo;
248 }
249
250 enum
251 {
252         H_CONTENT_TRANSFER_ENCODING = 0,
253         H_CONTENT_TYPE              = 1,
254         H_CONTENT_DISPOSITION       = 2,
255         H_CONTENT_DESCRIPTION       = 3,
256         H_SUBJECT                   = 4
257 };
258
259 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
260 {
261         const gchar *value;
262
263         g_return_val_if_fail(mimeinfo != NULL, NULL);
264         g_return_val_if_fail(name != NULL, NULL);
265
266         value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
267         if (value == NULL)
268                 value = g_hash_table_lookup(mimeinfo->typeparameters, name);
269         
270         return value;
271 }
272
273 gboolean procmime_decode_content(MimeInfo *mimeinfo)
274 {
275         gchar buf[BUFFSIZE];
276         gint readend;
277         gchar *tmpfilename;
278         FILE *outfp, *infp;
279         struct stat statbuf;
280         gboolean tmp_file = FALSE;
281
282         EncodingType encoding = forced_encoding 
283                                 ? forced_encoding
284                                 : mimeinfo->encoding_type;
285                    
286         g_return_val_if_fail(mimeinfo != NULL, FALSE);
287
288         if (encoding == ENC_UNKNOWN ||
289             encoding == ENC_BINARY)
290                 return TRUE;
291
292         infp = fopen(mimeinfo->data.filename, "rb");
293         if (!infp) {
294                 perror("fopen");
295                 return FALSE;
296         }
297         fseek(infp, mimeinfo->offset, SEEK_SET);
298
299         outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
300         if (!outfp) {
301                 perror("tmpfile");
302                 return FALSE;
303         }
304         tmp_file = TRUE;
305         readend = mimeinfo->offset + mimeinfo->length;
306
307         if (encoding == ENC_QUOTED_PRINTABLE) {
308                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
309                         gint len;
310                         len = qp_decode_line(buf);
311                         fwrite(buf, len, 1, outfp);
312                 }
313         } else if (encoding == ENC_BASE64) {
314                 gchar outbuf[BUFFSIZE];
315                 gint len;
316                 Base64Decoder *decoder;
317                 gboolean uncanonicalize = FALSE;
318                 FILE *tmpfp = outfp;
319
320                 if (mimeinfo->type == MIMETYPE_TEXT ||
321                     mimeinfo->type == MIMETYPE_MESSAGE) {
322                         uncanonicalize = TRUE;
323                         tmpfp = my_tmpfile();
324                         if (!tmpfp) {
325                                 perror("tmpfile");
326                                 if (tmp_file) fclose(outfp);
327                                 return FALSE;
328                         }
329                 }
330
331                 decoder = base64_decoder_new();
332                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
333                         len = base64_decoder_decode(decoder, buf, outbuf);
334                         if (len < 0) {
335                                 g_warning("Bad BASE64 content\n");
336                                 break;
337                         }
338                         fwrite(outbuf, sizeof(gchar), len, tmpfp);
339                 }
340                 base64_decoder_free(decoder);
341
342                 if (uncanonicalize) {
343                         rewind(tmpfp);
344                         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
345                                 strcrchomp(buf);
346                                 fputs(buf, outfp);
347                         }
348                         fclose(tmpfp);
349                 }
350         } else if (encoding == ENC_X_UUENCODE) {
351                 gchar outbuf[BUFFSIZE];
352                 gint len;
353                 gboolean flag = FALSE;
354
355                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
356                         if (!flag && strncmp(buf,"begin ", 6)) continue;
357
358                         if (flag) {
359                                 len = fromuutobits(outbuf, buf);
360                                 if (len <= 0) {
361                                         if (len < 0) 
362                                                 g_warning("Bad UUENCODE content(%d)\n", len);
363                                         break;
364                                 }
365                                 fwrite(outbuf, sizeof(gchar), len, outfp);
366                         } else
367                                 flag = TRUE;
368                 }
369         } else {
370                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
371                         fputs(buf, outfp);
372                 }
373         }
374
375         fclose(outfp);
376         fclose(infp);
377
378         stat(tmpfilename, &statbuf);
379         if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
380                 unlink(mimeinfo->data.filename);
381         if (mimeinfo->data.filename != NULL)
382                 g_free(mimeinfo->data.filename);
383         mimeinfo->data.filename = tmpfilename;
384         mimeinfo->tmp = TRUE;
385         mimeinfo->offset = 0;
386         mimeinfo->length = statbuf.st_size;
387         mimeinfo->encoding_type = ENC_BINARY;
388
389         return TRUE;
390 }
391
392 #define B64_LINE_SIZE           57
393 #define B64_BUFFSIZE            77
394
395 gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
396 {
397         FILE *infp, *outfp;
398         gint len;
399         gchar *tmpfilename;
400         struct stat statbuf;
401
402         if (mimeinfo->encoding_type != ENC_UNKNOWN &&
403             mimeinfo->encoding_type != ENC_BINARY &&
404             mimeinfo->encoding_type != ENC_7BIT &&
405             mimeinfo->encoding_type != ENC_8BIT)
406                 if(!procmime_decode_content(mimeinfo))
407                         return FALSE;
408
409         outfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpfilename);
410         if (!outfp) {
411                 perror("tmpfile");
412                 return FALSE;
413         }
414
415         if (mimeinfo->content == MIMECONTENT_FILE) {
416                 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
417                         g_warning("Can't open file %s\n", mimeinfo->data.filename);
418                         return FALSE;
419                 }
420         } else if (mimeinfo->content == MIMECONTENT_MEM) {
421                 infp = str_open_as_stream(mimeinfo->data.mem);
422                 if (infp == NULL)
423                         return FALSE;
424         }
425
426         if (encoding == ENC_BASE64) {
427                 gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
428                 FILE *tmp_fp = infp;
429                 gchar *tmp_file = NULL;
430
431                 if (mimeinfo->type == MIMETYPE_TEXT ||
432                      mimeinfo->type == MIMETYPE_MESSAGE) {
433                         if (mimeinfo->content == MIMECONTENT_FILE) {
434                                 tmp_file = get_tmp_file();
435                                 if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
436                                         g_free(tmp_file);
437                                         fclose(infp);
438                                         return FALSE;
439                                 }
440                                 if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
441                                         FILE_OP_ERROR(tmp_file, "fopen");
442                                         unlink(tmp_file);
443                                         g_free(tmp_file);
444                                         fclose(infp);
445                                         return FALSE;
446                                 }
447                         } else {
448                                 gchar *out = canonicalize_str(mimeinfo->data.mem);
449                                 fclose(infp);
450                                 infp = str_open_as_stream(out);
451                                 g_free(out);
452                                 if (infp == NULL)
453                                         return FALSE;
454                         }
455                 }
456
457                 while ((len = fread(inbuf, sizeof(gchar),
458                                     B64_LINE_SIZE, tmp_fp))
459                        == B64_LINE_SIZE) {
460                         base64_encode(outbuf, inbuf, B64_LINE_SIZE);
461                         fputs(outbuf, outfp);
462                         fputc('\n', outfp);
463                 }
464                 if (len > 0 && feof(tmp_fp)) {
465                         base64_encode(outbuf, inbuf, len);
466                         fputs(outbuf, outfp);
467                         fputc('\n', outfp);
468                 }
469
470                 if (tmp_file) {
471                         fclose(tmp_fp);
472                         unlink(tmp_file);
473                         g_free(tmp_file);
474                 }
475         } else if (encoding == ENC_QUOTED_PRINTABLE) {
476                 gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
477
478                 while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
479                         qp_encode_line(outbuf, inbuf);
480                         fputs(outbuf, outfp);
481                 }
482         } else {
483                 gchar buf[BUFFSIZE];
484
485                 while (fgets(buf, sizeof(buf), infp) != NULL) {
486                         strcrchomp(buf);
487                         fputs(buf, outfp);
488                 }
489         }
490
491         fclose(outfp);
492         fclose(infp);
493
494         if (mimeinfo->content == MIMECONTENT_FILE) {
495                 if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
496                         unlink(mimeinfo->data.filename);
497                 g_free(mimeinfo->data.filename);
498         } else if (mimeinfo->content == MIMECONTENT_MEM) {
499                 if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
500                         g_free(mimeinfo->data.mem);
501         }
502
503         stat(tmpfilename, &statbuf);
504         mimeinfo->content = MIMECONTENT_FILE;
505         mimeinfo->data.filename = tmpfilename;
506         mimeinfo->tmp = TRUE;
507         mimeinfo->offset = 0;
508         mimeinfo->length = statbuf.st_size;
509         mimeinfo->encoding_type = encoding;
510
511         return TRUE;
512 }
513
514 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
515 {
516         FILE *infp, *outfp;
517         gchar buf[BUFFSIZE];
518         gint restlength, readlength;
519
520         g_return_val_if_fail(outfile != NULL, -1);
521         g_return_val_if_fail(mimeinfo != NULL, -1);
522
523         if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
524                 return -1;
525
526         if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
527                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
528                 return -1;
529         }
530         if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
531                 FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
532                 fclose(infp);
533                 return -1;
534         }
535         if ((outfp = fopen(outfile, "wb")) == NULL) {
536                 FILE_OP_ERROR(outfile, "fopen");
537                 fclose(infp);
538                 return -1;
539         }
540
541         restlength = mimeinfo->length;
542
543         while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
544                 fwrite(buf, 1, readlength, outfp);
545                 restlength -= readlength;
546         }
547
548         fclose(infp);
549         if (fclose(outfp) == EOF) {
550                 FILE_OP_ERROR(outfile, "fclose");
551                 unlink(outfile);
552                 return -1;
553         }
554
555         return 0;
556 }
557
558 struct ContentRenderer {
559         char * content_type;
560         char * renderer;
561 };
562
563 static GList * renderer_list = NULL;
564
565 static struct ContentRenderer *
566 content_renderer_new(char * content_type, char * renderer)
567 {
568         struct ContentRenderer * cr;
569
570         cr = g_new(struct ContentRenderer, 1);
571         if (cr == NULL)
572                 return NULL;
573
574         cr->content_type = g_strdup(content_type);
575         cr->renderer = g_strdup(renderer);
576
577         return cr;
578 }
579
580 static void content_renderer_free(struct ContentRenderer * cr)
581 {
582         g_free(cr->content_type);
583         g_free(cr->renderer);
584         g_free(cr);
585 }
586
587 void renderer_read_config(void)
588 {
589         gchar buf[BUFFSIZE];
590         FILE * f;
591         gchar * rcpath;
592
593         g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
594         renderer_list = NULL;
595
596         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
597         f = fopen(rcpath, "rb");
598         g_free(rcpath);
599         
600         if (f == NULL)
601                 return;
602
603         while (fgets(buf, BUFFSIZE, f)) {
604                 char * p;
605                 struct ContentRenderer * cr;
606
607                 strretchomp(buf);
608                 p = strchr(buf, ' ');
609                 if (p == NULL)
610                         continue;
611                 * p = 0;
612
613                 cr = content_renderer_new(buf, p + 1);
614                 if (cr == NULL)
615                         continue;
616
617                 renderer_list = g_list_append(renderer_list, cr);
618         }
619
620         fclose(f);
621 }
622
623 void renderer_write_config(void)
624 {
625         gchar * rcpath;
626         PrefFile *pfile;
627         GList * cur;
628
629         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
630         
631         if ((pfile = prefs_write_open(rcpath)) == NULL) {
632                 g_warning("failed to write configuration to file\n");
633                 g_free(rcpath);
634                 return;
635         }
636
637         g_free(rcpath);
638
639         for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
640                 struct ContentRenderer * renderer;
641                 renderer = cur->data;
642                 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
643                         renderer->renderer);
644         }
645
646         if (prefs_file_close(pfile) < 0) {
647                 g_warning("failed to write configuration to file\n");
648                 return;
649         }
650 }
651
652 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
653 {
654         FILE *tmpfp, *outfp;
655         const gchar *src_codeset;
656         gboolean conv_fail = FALSE;
657         gchar buf[BUFFSIZE];
658         gchar *str;
659         struct ContentRenderer * renderer;
660         GList * cur;
661         gchar *tmpfile, *content_type;
662     
663         g_return_val_if_fail(mimeinfo != NULL, NULL);
664
665         if (!procmime_decode_content(mimeinfo))
666                 return NULL;
667
668         tmpfile = procmime_get_tmp_file_name(mimeinfo);
669         if (tmpfile == NULL)
670                 return NULL;
671
672         if (procmime_get_part(tmpfile, mimeinfo) < 0) {
673                 g_free(tmpfile);
674                 return NULL;
675         }
676
677         tmpfp = fopen(tmpfile, "rb");
678         if (tmpfp == NULL) {
679                 g_free(tmpfile);
680                 return NULL;
681         }
682
683         if ((outfp = my_tmpfile()) == NULL) {
684                 perror("tmpfile");
685                 fclose(tmpfp);
686                 g_free(tmpfile);
687                 return NULL;
688         }
689
690         src_codeset = forced_charset
691                       ? forced_charset : 
692                       procmime_mimeinfo_get_parameter(mimeinfo, "charset");
693
694         renderer = NULL;
695
696         content_type = procmime_get_content_type_str(mimeinfo->type,
697                                                      mimeinfo->subtype);
698         for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
699                 struct ContentRenderer * cr;
700
701                 cr = cur->data;
702                 if (g_ascii_strcasecmp(cr->content_type, content_type) == 0) {
703                         renderer = cr;
704                         break;
705                 }
706         }
707         g_free(content_type);
708
709         if (renderer != NULL) {
710                 FILE * p;
711                 int oldout;
712                 
713                 oldout = dup(1);
714                 
715                 dup2(fileno(outfp), 1);
716                 
717                 p = popen(renderer->renderer, "w");
718                 if (p != NULL) {
719                         size_t count;
720                         
721                         while ((count =
722                                 fread(buf, sizeof(char), sizeof(buf),
723                                       tmpfp)) > 0)
724                                 fwrite(buf, sizeof(char), count, p);
725                         pclose(p);
726                 }
727                 
728                 dup2(oldout, 1);
729 #warning FIXME_GTK2 HTML/RTF not yet utf8
730 /* CodeConverter seems to have no effect here */
731         } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
732                 HTMLParser *parser;
733                 CodeConverter *conv;
734
735                 conv = conv_code_converter_new(src_codeset);
736                 parser = html_parser_new(tmpfp, conv);
737                 while ((str = html_parse(parser)) != NULL) {
738                         fputs(str, outfp);
739                 }
740                 html_parser_destroy(parser);
741                 conv_code_converter_destroy(conv);
742         } else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
743                 ERTFParser *parser;
744                 CodeConverter *conv;
745
746                 conv = conv_code_converter_new(src_codeset);
747                 parser = ertf_parser_new(tmpfp, conv);
748                 while ((str = ertf_parse(parser)) != NULL) {
749                         fputs(str, outfp);
750                 }
751                 ertf_parser_destroy(parser);
752                 conv_code_converter_destroy(conv);
753         } else if (mimeinfo->type == MIMETYPE_TEXT) {
754                 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
755                         str = conv_codeset_strdup(buf, src_codeset, CS_UTF_8);
756                         if (str) {
757                                 fputs(str, outfp);
758                                 g_free(str);
759                         } else {
760                                 conv_fail = TRUE;
761                                 fputs(buf, outfp);
762                         }
763                 }
764         }
765
766         if (conv_fail)
767                 g_warning("procmime_get_text_content(): Code conversion failed.\n");
768
769         fclose(tmpfp);
770         rewind(outfp);
771         unlink(tmpfile);
772         g_free(tmpfile);
773
774         return outfp;
775 }
776
777 /* search the first text part of (multipart) MIME message,
778    decode, convert it and output to outfp. */
779 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
780 {
781         FILE *outfp = NULL;
782         MimeInfo *mimeinfo, *partinfo;
783
784         g_return_val_if_fail(msginfo != NULL, NULL);
785
786         mimeinfo = procmime_scan_message(msginfo);
787         if (!mimeinfo) return NULL;
788
789         partinfo = mimeinfo;
790         while (partinfo && partinfo->type != MIMETYPE_TEXT)
791                 partinfo = procmime_mimeinfo_next(partinfo);
792
793         if (partinfo)
794                 outfp = procmime_get_text_content(partinfo);
795
796         procmime_mimeinfo_free_all(mimeinfo);
797
798         return outfp;
799 }
800
801 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
802                                    const gchar *str, StrFindFunc find_func)
803 {
804         FILE *outfp;
805         gchar buf[BUFFSIZE];
806
807         g_return_val_if_fail(mimeinfo != NULL, FALSE);
808         g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
809         g_return_val_if_fail(str != NULL, FALSE);
810         g_return_val_if_fail(find_func != NULL, FALSE);
811
812         outfp = procmime_get_text_content(mimeinfo);
813
814         if (!outfp)
815                 return FALSE;
816
817         while (fgets(buf, sizeof(buf), outfp) != NULL) {
818                 strretchomp(buf);
819                 if (find_func(buf, str)) {
820                         fclose(outfp);
821                         return TRUE;
822                 }
823         }
824
825         fclose(outfp);
826
827         return FALSE;
828 }
829
830 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
831                               StrFindFunc find_func)
832 {
833         MimeInfo *mimeinfo;
834         MimeInfo *partinfo;
835         gchar *filename;
836         gboolean found = FALSE;
837
838         g_return_val_if_fail(msginfo != NULL, FALSE);
839         g_return_val_if_fail(str != NULL, FALSE);
840         g_return_val_if_fail(find_func != NULL, FALSE);
841
842         filename = procmsg_get_message_file(msginfo);
843         if (!filename) return FALSE;
844         mimeinfo = procmime_scan_message(msginfo);
845
846         for (partinfo = mimeinfo; partinfo != NULL;
847              partinfo = procmime_mimeinfo_next(partinfo)) {
848                 if (partinfo->type == MIMETYPE_TEXT) {
849                         if (procmime_find_string_part
850                                 (partinfo, filename, str, find_func) == TRUE) {
851                                 found = TRUE;
852                                 break;
853                         }
854                 }
855         }
856
857         procmime_mimeinfo_free_all(mimeinfo);
858         g_free(filename);
859
860         return found;
861 }
862
863 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
864 {
865         static guint32 id = 0;
866         gchar *base;
867         gchar *filename;
868         gchar f_prefix[10];
869
870         g_return_val_if_fail(mimeinfo != NULL, NULL);
871
872         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
873
874         if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
875                 base = g_strdup("mimetmp.html");
876         else {
877                 const gchar *basetmp;
878
879                 basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
880                 if (basetmp == NULL)
881                         basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
882                 if (basetmp == NULL)
883                         basetmp = "mimetmp";
884                 base = g_path_get_basename(basetmp);
885                 if (*base == '\0') base = g_strdup("mimetmp");
886                 subst_for_shellsafe_filename(base);
887         }
888
889         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
890                                f_prefix, base, NULL);
891
892         g_free(base);
893         return filename;
894 }
895
896 static GList *mime_type_list = NULL;
897
898 gchar *procmime_get_mime_type(const gchar *filename)
899 {
900         static GHashTable *mime_type_table = NULL;
901         MimeType *mime_type;
902         const gchar *p;
903         gchar *ext;
904         gchar *base;
905
906         if (!mime_type_table) {
907                 mime_type_table = procmime_get_mime_type_table();
908                 if (!mime_type_table) return NULL;
909         }
910
911         base = g_path_get_basename(filename);
912         p = strrchr(base, '.');
913         g_free(base);
914         if (!p) return NULL;
915
916         Xstrdup_a(ext, p + 1, return NULL);
917         g_strdown(ext);
918         mime_type = g_hash_table_lookup(mime_type_table, ext);
919         if (mime_type) {
920                 gchar *str;
921
922                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
923                                   NULL);
924                 return str;
925         }
926
927         return NULL;
928 }
929
930 static guint procmime_str_hash(gconstpointer gptr)
931 {
932         guint hash_result = 0;
933         const char *str;
934
935         for (str = gptr; str && *str; str++) {
936                 if (isupper(*str)) hash_result += (*str + ' ');
937                 else hash_result += *str;
938         }
939
940         return hash_result;
941 }
942
943 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
944 {
945         const char *str1 = gptr1;
946         const char *str2 = gptr2;
947
948         return !g_utf8_collate(str1, str2);
949 }
950
951 static GHashTable *procmime_get_mime_type_table(void)
952 {
953         GHashTable *table = NULL;
954         GList *cur;
955         MimeType *mime_type;
956         gchar **exts;
957
958         if (!mime_type_list) {
959                 mime_type_list = procmime_get_mime_type_list();
960                 if (!mime_type_list) return NULL;
961         }
962
963         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
964
965         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
966                 gint i;
967                 gchar *key;
968
969                 mime_type = (MimeType *)cur->data;
970
971                 if (!mime_type->extension) continue;
972
973                 exts = g_strsplit(mime_type->extension, " ", 16);
974                 for (i = 0; exts[i] != NULL; i++) {
975                         /* make the key case insensitive */
976                         g_strdown(exts[i]);
977                         /* use previously dup'd key on overwriting */
978                         if (g_hash_table_lookup(table, exts[i]))
979                                 key = exts[i];
980                         else
981                                 key = g_strdup(exts[i]);
982                         g_hash_table_insert(table, key, mime_type);
983                 }
984                 g_strfreev(exts);
985         }
986
987         return table;
988 }
989
990 GList *procmime_get_mime_type_list(void)
991 {
992         GList *list = NULL;
993         FILE *fp;
994         gchar buf[BUFFSIZE];
995         guchar *p;
996         gchar *delim;
997         MimeType *mime_type;
998
999         if (mime_type_list) 
1000                 return mime_type_list;
1001
1002         if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
1003                 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
1004                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
1005                         return NULL;
1006                 }
1007         }
1008
1009         while (fgets(buf, sizeof(buf), fp) != NULL) {
1010                 p = strchr(buf, '#');
1011                 if (p) *p = '\0';
1012                 g_strstrip(buf);
1013
1014                 p = buf;
1015                 while (*p && !isspace(*p)) p++;
1016                 if (*p) {
1017                         *p = '\0';
1018                         p++;
1019                 }
1020                 delim = strchr(buf, '/');
1021                 if (delim == NULL) continue;
1022                 *delim = '\0';
1023
1024                 mime_type = g_new(MimeType, 1);
1025                 mime_type->type = g_strdup(buf);
1026                 mime_type->sub_type = g_strdup(delim + 1);
1027
1028                 while (*p && isspace(*p)) p++;
1029                 if (*p)
1030                         mime_type->extension = g_strdup(p);
1031                 else
1032                         mime_type->extension = NULL;
1033
1034                 list = g_list_append(list, mime_type);
1035         }
1036
1037         fclose(fp);
1038
1039         if (!list)
1040                 g_warning("Can't read mime.types\n");
1041
1042         return list;
1043 }
1044
1045 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1046 {
1047         if (!charset)
1048                 return ENC_8BIT;
1049         else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1050                  !g_ascii_strcasecmp(charset, "US-ASCII"))
1051                 return ENC_7BIT;
1052         else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1053                  !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1054                  !g_ascii_strcasecmp(charset, "Windows-1251"))
1055                 return ENC_8BIT;
1056         else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1057                 return ENC_QUOTED_PRINTABLE;
1058         else
1059                 return ENC_8BIT;
1060 }
1061
1062 EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1063 {
1064         FILE *fp;
1065         guchar buf[BUFFSIZE];
1066         size_t len;
1067         size_t octet_chars = 0;
1068         size_t total_len = 0;
1069         gfloat octet_percentage;
1070
1071         if ((fp = fopen(file, "rb")) == NULL) {
1072                 FILE_OP_ERROR(file, "fopen");
1073                 return ENC_UNKNOWN;
1074         }
1075
1076         while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1077                 guchar *p;
1078                 gint i;
1079
1080                 for (p = buf, i = 0; i < len; ++p, ++i) {
1081                         if (*p & 0x80)
1082                                 ++octet_chars;
1083                 }
1084                 total_len += len;
1085         }
1086
1087         fclose(fp);
1088         
1089         if (total_len > 0)
1090                 octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1091         else
1092                 octet_percentage = 0.0;
1093
1094         debug_print("procmime_get_encoding_for_text_file(): "
1095                     "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1096                     100.0 * octet_percentage);
1097
1098         if (octet_percentage > 0.20) {
1099                 debug_print("using BASE64\n");
1100                 return ENC_BASE64;
1101         } else if (octet_chars > 0) {
1102                 debug_print("using quoted-printable\n");
1103                 return ENC_QUOTED_PRINTABLE;
1104         } else {
1105                 debug_print("using 7bit\n");
1106                 return ENC_7BIT;
1107         }
1108 }
1109
1110 struct EncodingTable 
1111 {
1112         gchar *str;
1113         EncodingType enc_type;
1114 };
1115
1116 struct EncodingTable encoding_table[] = {
1117         {"7bit", ENC_7BIT},
1118         {"8bit", ENC_8BIT},
1119         {"binary", ENC_BINARY},
1120         {"quoted-printable", ENC_QUOTED_PRINTABLE},
1121         {"base64", ENC_BASE64},
1122         {"x-uuencode", ENC_UNKNOWN},
1123         {NULL, ENC_UNKNOWN},
1124 };
1125
1126 const gchar *procmime_get_encoding_str(EncodingType encoding)
1127 {
1128         struct EncodingTable *enc_table;
1129         
1130         for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1131                 if (enc_table->enc_type == encoding)
1132                         return enc_table->str;
1133         }
1134         return NULL;
1135 }
1136
1137 /* --- NEW MIME STUFF --- */
1138 struct TypeTable
1139 {
1140         gchar *str;
1141         MimeMediaType type;
1142 };
1143
1144 static struct TypeTable mime_type_table[] = {
1145         {"text", MIMETYPE_TEXT},
1146         {"image", MIMETYPE_IMAGE},
1147         {"audio", MIMETYPE_AUDIO},
1148         {"video", MIMETYPE_VIDEO},
1149         {"application", MIMETYPE_APPLICATION},
1150         {"message", MIMETYPE_MESSAGE},
1151         {"multipart", MIMETYPE_MULTIPART},
1152         {NULL, 0},
1153 };
1154
1155 const gchar *procmime_get_media_type_str(MimeMediaType type)
1156 {
1157         struct TypeTable *type_table;
1158         
1159         for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
1160                 if (type_table->type == type)
1161                         return type_table->str;
1162         }
1163         return NULL;
1164 }
1165
1166 MimeMediaType procmime_get_media_type(const gchar *str)
1167 {
1168         struct TypeTable *typetablearray;
1169
1170         for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
1171                 if (g_ascii_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
1172                         return typetablearray->type;
1173
1174         return MIMETYPE_UNKNOWN;
1175 }
1176
1177 /*!
1178  *\brief        Safe wrapper for content type string.
1179  *
1180  *\return       const gchar * Pointer to content type string. 
1181  */
1182 gchar *procmime_get_content_type_str(MimeMediaType type,
1183                                            const char *subtype)
1184 {
1185         const gchar *type_str = NULL;
1186
1187         if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
1188                 return g_strdup("unknown");
1189         return g_strdup_printf("%s/%s", type_str, subtype);
1190 }
1191
1192 void procmime_parse_mimepart(MimeInfo *parent,
1193                              gchar *content_type,
1194                              gchar *content_encoding,
1195                              gchar *content_description,
1196                              gchar *content_id,
1197                              gchar *content_disposition,
1198                              const gchar *filename,
1199                              guint offset,
1200                              guint length);
1201
1202 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
1203 {
1204         HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
1205                                 {"Content-Transfer-Encoding:",
1206                                                    NULL, FALSE},
1207                                 {"Content-Description:",
1208                                                    NULL, TRUE},
1209                                 {"Content-ID:",
1210                                                    NULL, TRUE},
1211                                 {"Content-Disposition:",
1212                                                    NULL, TRUE},
1213                                 {"MIME-Version:",
1214                                                    NULL, TRUE},
1215                                 {NULL,             NULL, FALSE}};
1216         guint content_start, i;
1217         FILE *fp;
1218         gint mime_major, mime_minor;
1219
1220         procmime_decode_content(mimeinfo);
1221
1222         fp = fopen(mimeinfo->data.filename, "rb");
1223         if (fp == NULL) {
1224                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1225                 return;
1226         }
1227         fseek(fp, mimeinfo->offset, SEEK_SET);
1228         procheader_get_header_fields(fp, hentry);
1229         if (hentry[0].body != NULL)
1230                 conv_unmime_header_overwrite(hentry[0].body);
1231         if (hentry[2].body != NULL)
1232                 conv_unmime_header_overwrite(hentry[2].body);
1233         if (hentry[4].body != NULL)
1234                 conv_unmime_header_overwrite(hentry[4].body);
1235         content_start = ftell(fp);
1236         fclose(fp);
1237
1238         if ((hentry[5].body != NULL) &&
1239             (sscanf(hentry[5].body, "%d.%d", &mime_major, &mime_minor) == 2) &&
1240             (mime_major == 1) && (mime_minor == 0)) {
1241                 procmime_parse_mimepart(mimeinfo,
1242                                         hentry[0].body, hentry[1].body,
1243                                         hentry[2].body, hentry[3].body, 
1244                                         hentry[4].body, 
1245                                         mimeinfo->data.filename, content_start,
1246                                         mimeinfo->length - (content_start - mimeinfo->offset));
1247         } else {
1248                 MimeInfo *subinfo;
1249
1250                 subinfo = procmime_mimeinfo_new();
1251                 subinfo->content = MIMECONTENT_FILE;
1252                 subinfo->encoding_type = ENC_UNKNOWN;
1253                 subinfo->type = MIMETYPE_TEXT;
1254                 subinfo->subtype = g_strdup("plain");
1255                 subinfo->data.filename = g_strdup(mimeinfo->data.filename);
1256                 subinfo->offset = content_start;
1257                 subinfo->length = mimeinfo->length - (content_start - mimeinfo->offset);
1258
1259                 g_node_append(mimeinfo->node, subinfo->node);
1260         }
1261         for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1262                 g_free(hentry[i].body);
1263                 hentry[i].body = NULL;
1264         }
1265 }
1266
1267 void procmime_parse_multipart(MimeInfo *mimeinfo)
1268 {
1269         HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
1270                                 {"Content-Transfer-Encoding:",
1271                                                    NULL, FALSE},
1272                                 {"Content-Description:",
1273                                                    NULL, TRUE},
1274                                 {"Content-ID:",
1275                                                    NULL, TRUE},
1276                                 {"Content-Disposition:",
1277                                                    NULL, TRUE},
1278                                 {NULL,             NULL, FALSE}};
1279         gchar *p;
1280         gchar *boundary;
1281         gint boundary_len = 0, lastoffset = -1, i;
1282         gchar buf[BUFFSIZE];
1283         FILE *fp;
1284
1285         boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1286         if (!boundary)
1287                 return;
1288         boundary_len = strlen(boundary);
1289
1290         procmime_decode_content(mimeinfo);
1291
1292         fp = fopen(mimeinfo->data.filename, "rb");
1293         if (fp == NULL) {
1294                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1295                 return;
1296         }
1297         fseek(fp, mimeinfo->offset, SEEK_SET);
1298         while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1299                 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1300                         break;
1301
1302                 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1303                         if (lastoffset != -1) {
1304                                 procmime_parse_mimepart(mimeinfo,
1305                                                         hentry[0].body, hentry[1].body,
1306                                                         hentry[2].body, hentry[3].body, 
1307                                                         hentry[4].body, 
1308                                                         mimeinfo->data.filename, lastoffset,
1309                                                         (ftell(fp) - strlen(buf)) - lastoffset - 1);
1310                         }
1311                         
1312                         if (buf[2 + boundary_len]     == '-' &&
1313                             buf[2 + boundary_len + 1] == '-')
1314                                 break;
1315
1316                         for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1317                                 g_free(hentry[i].body);
1318                                 hentry[i].body = NULL;
1319                         }
1320                         procheader_get_header_fields(fp, hentry);
1321                         if (hentry[0].body != NULL)
1322                                 conv_unmime_header_overwrite(hentry[0].body);
1323                         if (hentry[2].body != NULL)
1324                                 conv_unmime_header_overwrite(hentry[2].body);
1325                         if (hentry[4].body != NULL)
1326                                 conv_unmime_header_overwrite(hentry[4].body);
1327                         lastoffset = ftell(fp);
1328                 }
1329         }
1330         for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1331                 g_free(hentry[i].body);
1332                 hentry[i].body = NULL;
1333         }
1334         fclose(fp);
1335 }
1336
1337 static void parse_parameters(const gchar *parameters, GHashTable *table)
1338 {
1339         gchar *params, *param, *next;
1340         GSList *convlist = NULL, *concatlist = NULL, *cur;
1341
1342         params = g_strdup(parameters);
1343         param = params;
1344         next = params;
1345         for (; next != NULL; param = next) {
1346                 gchar *attribute, *value, *tmp;
1347                 gint len;
1348                 gboolean convert = FALSE;
1349
1350                 next = strchr_with_skip_quote(param, '"', ';');
1351                 if (next != NULL) {
1352                         next[0] = '\0';
1353                         next++;
1354                 }
1355
1356                 g_strstrip(param);
1357
1358                 attribute = param;
1359                 value = strchr(attribute, '=');
1360                 if (value == NULL)
1361                         continue;
1362
1363                 value[0] = '\0';
1364                 value++;
1365
1366                 g_strdown(attribute);
1367
1368                 len = strlen(attribute);
1369                 if (attribute[len - 1] == '*') {
1370                         gchar *srcpos, *dstpos, *endpos;
1371
1372                         convert = TRUE;
1373                         attribute[len - 1] = '\0';
1374
1375                         srcpos = value;
1376                         dstpos = value;
1377                         endpos = value + strlen(value);
1378                         while (srcpos < endpos) {
1379                                 if (*srcpos != '%')
1380                                         *dstpos = *srcpos;
1381                                 else {
1382                                         guchar dstvalue;
1383
1384                                         if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
1385                                                 *dstpos = '?';
1386                                         else
1387                                                 *dstpos = dstvalue;
1388                                         srcpos += 2;
1389                                 }
1390                                 srcpos++;
1391                                 dstpos++;
1392                         }
1393                         *dstpos = '\0';
1394                 } else {
1395                         if (value[0] == '"')
1396                                 extract_quote(value, '"');
1397                         else if ((tmp = strchr(value, ' ')) != NULL)
1398                                 *tmp = '\0';
1399                 }
1400
1401                 if (strrchr(attribute, '*') != NULL) {
1402                         gchar *tmpattr;
1403
1404                         tmpattr = g_strdup(attribute);
1405                         tmp = strrchr(tmpattr, '*');
1406                         tmp[0] = '\0';
1407
1408                         if ((tmp[1] == '0') && (tmp[2] == '\0') && 
1409                             (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
1410                                 concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
1411
1412                         if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
1413                                 convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
1414
1415                         g_free(tmpattr);
1416                 } else if (convert) {
1417                         if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
1418                                 convlist = g_slist_prepend(convlist, g_strdup(attribute));
1419                 }
1420
1421                 if (g_hash_table_lookup(table, attribute) == NULL)
1422                         g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
1423         }
1424
1425         for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
1426                 gchar *attribute, *attrwnum, *partvalue;
1427                 gint n = 0;
1428                 GString *value;
1429
1430                 attribute = (gchar *) cur->data;
1431                 value = g_string_sized_new(64);
1432
1433                 attrwnum = g_strdup_printf("%s*%d", attribute, n);
1434                 while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
1435                         g_string_append(value, partvalue);
1436
1437                         g_free(attrwnum);
1438                         n++;
1439                         attrwnum = g_strdup_printf("%s*%d", attribute, n);
1440                 }
1441                 g_free(attrwnum);
1442
1443                 g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
1444                 g_string_free(value, TRUE);
1445         }
1446         slist_free_strings(concatlist);
1447         g_slist_free(concatlist);
1448
1449         for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
1450                 gchar *attribute, *key, *value;
1451                 gchar *charset, *lang, *oldvalue, *newvalue;
1452
1453                 attribute = (gchar *) cur->data;
1454                 if (!g_hash_table_lookup_extended(table, attribute, (gpointer *) &key, (gpointer *) &value))
1455                         continue;
1456
1457                 charset = value;
1458                 lang = strchr(charset, '\'');
1459                 if (lang == NULL)
1460                         continue;
1461                 lang[0] = '\0';
1462                 lang++;
1463                 oldvalue = strchr(lang, '\'');
1464                 if (oldvalue == NULL)
1465                         continue;
1466                 oldvalue[0] = '\0';
1467                 oldvalue++;
1468
1469                 newvalue = conv_codeset_strdup(oldvalue, charset, CS_UTF_8);
1470
1471                 g_hash_table_remove(table, attribute);
1472                 g_free(key);
1473                 g_free(value);
1474
1475                 g_hash_table_insert(table, g_strdup(attribute), newvalue);
1476         }
1477         slist_free_strings(convlist);
1478         g_slist_free(convlist);
1479
1480         g_free(params);
1481 }       
1482
1483 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1484 {
1485         g_return_if_fail(content_type != NULL);
1486         g_return_if_fail(mimeinfo != NULL);
1487
1488         /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
1489          * if it's not available we use the default Content-Type */
1490         if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
1491                 mimeinfo->type = MIMETYPE_TEXT;
1492                 mimeinfo->subtype = g_strdup("plain");
1493                 if (g_hash_table_lookup(mimeinfo->typeparameters,
1494                                        "charset") == NULL)
1495                         g_hash_table_insert(mimeinfo->typeparameters,
1496                                             g_strdup("charset"),
1497                                             g_strdup("us-ascii"));
1498         } else {
1499                 gchar *type, *subtype, *params;
1500
1501                 type = g_strdup(content_type);
1502                 subtype = strchr(type, '/') + 1;
1503                 *(subtype - 1) = '\0';
1504                 if ((params = strchr(subtype, ';')) != NULL) {
1505                         params[0] = '\0';
1506                         params++;
1507                 }
1508
1509                 mimeinfo->type = procmime_get_media_type(type);
1510                 mimeinfo->subtype = g_strdup(subtype);
1511
1512                 /* Get mimeinfo->typeparameters */
1513                 if (params != NULL)
1514                         parse_parameters(params, mimeinfo->typeparameters);
1515
1516                 g_free(type);
1517         }
1518 }
1519
1520 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1521 {
1522         gchar *tmp, *params;
1523
1524         g_return_if_fail(content_disposition != NULL);
1525         g_return_if_fail(mimeinfo != NULL);
1526
1527         tmp = g_strdup(content_disposition);
1528         if ((params = strchr(tmp, ';')) != NULL) {
1529                 params[0] = '\0';
1530                 params++;
1531         }       
1532         g_strstrip(tmp);
1533
1534         if (!g_ascii_strcasecmp(tmp, "inline")) 
1535                 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1536         else if (!g_ascii_strcasecmp(tmp, "attachment"))
1537                 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1538         else
1539                 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1540         
1541         if (params != NULL)
1542                 parse_parameters(params, mimeinfo->dispositionparameters);
1543
1544         g_free(tmp);
1545 }
1546
1547
1548 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1549 {
1550         struct EncodingTable *enc_table;
1551         
1552         for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1553                 if (g_ascii_strcasecmp(enc_table->str, content_encoding) == 0) {
1554                         mimeinfo->encoding_type = enc_table->enc_type;
1555                         return;
1556                 }
1557         }
1558         mimeinfo->encoding_type = ENC_UNKNOWN;
1559         return;
1560 }
1561
1562 void procmime_parse_mimepart(MimeInfo *parent,
1563                              gchar *content_type,
1564                              gchar *content_encoding,
1565                              gchar *content_description,
1566                              gchar *content_id,
1567                              gchar *content_disposition,
1568                              const gchar *filename,
1569                              guint offset,
1570                              guint length)
1571 {
1572         MimeInfo *mimeinfo;
1573
1574         /* Create MimeInfo */
1575         mimeinfo = procmime_mimeinfo_new();
1576         mimeinfo->content = MIMECONTENT_FILE;
1577         if (parent != NULL)
1578                 g_node_append(parent->node, mimeinfo->node);
1579         mimeinfo->data.filename = g_strdup(filename);
1580         mimeinfo->offset = offset;
1581         mimeinfo->length = length;
1582
1583         if (content_type != NULL) {
1584                 procmime_parse_content_type(content_type, mimeinfo);
1585         } else {
1586                 mimeinfo->type = MIMETYPE_TEXT;
1587                 mimeinfo->subtype = g_strdup("plain");
1588                 if (g_hash_table_lookup(mimeinfo->typeparameters,
1589                                        "charset") == NULL)
1590                         g_hash_table_insert(mimeinfo->typeparameters, g_strdup("charset"), g_strdup("us-ascii"));
1591         }
1592
1593         if (content_encoding != NULL) {
1594                 procmime_parse_content_encoding(content_encoding, mimeinfo);
1595         } else {
1596                 mimeinfo->encoding_type = ENC_UNKNOWN;
1597         }
1598
1599         if (content_description != NULL)
1600                 mimeinfo->description = g_strdup(content_description);
1601         else
1602                 mimeinfo->description = NULL;
1603
1604         if (content_id != NULL)
1605                 mimeinfo->id = g_strdup(content_id);
1606         else
1607                 mimeinfo->id = NULL;
1608
1609         if (content_disposition != NULL) 
1610                 procmime_parse_content_disposition(content_disposition, mimeinfo);
1611         else
1612                 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1613
1614         /* Call parser for mime type */
1615         switch (mimeinfo->type) {
1616                 case MIMETYPE_MESSAGE:
1617                         if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1618                                 procmime_parse_message_rfc822(mimeinfo);
1619                         }
1620                         break;
1621                         
1622                 case MIMETYPE_MULTIPART:
1623                         procmime_parse_multipart(mimeinfo);
1624                         break;
1625                         
1626                 default:
1627                         break;
1628         }
1629 }
1630
1631 static gchar *typenames[] = {
1632     "text",
1633     "image",
1634     "audio",
1635     "video",
1636     "application",
1637     "message",
1638     "multipart",
1639     "unknown",
1640 };
1641
1642 static gboolean output_func(GNode *node, gpointer data)
1643 {
1644         guint i, depth;
1645         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1646
1647         depth = g_node_depth(node);
1648         for (i = 0; i < depth; i++)
1649                 printf("    ");
1650         printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1651
1652         return FALSE;
1653 }
1654
1655 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1656 {
1657         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1658 }
1659
1660 MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1661 {
1662         MimeInfo *mimeinfo;
1663         struct stat buf;
1664
1665         stat(filename, &buf);
1666
1667         mimeinfo = procmime_mimeinfo_new();
1668         mimeinfo->content = MIMECONTENT_FILE;
1669         mimeinfo->encoding_type = ENC_UNKNOWN;
1670         mimeinfo->type = MIMETYPE_MESSAGE;
1671         mimeinfo->subtype = g_strdup("rfc822");
1672         mimeinfo->data.filename = g_strdup(filename);
1673         mimeinfo->offset = offset;
1674         mimeinfo->length = buf.st_size - offset;
1675
1676         procmime_parse_message_rfc822(mimeinfo);
1677         if (debug_get_mode())
1678                 output_mime_structure(mimeinfo, 0);
1679
1680         return mimeinfo;
1681 }
1682
1683 MimeInfo *procmime_scan_file(const gchar *filename)
1684 {
1685         MimeInfo *mimeinfo;
1686
1687         g_return_val_if_fail(filename != NULL, NULL);
1688
1689         mimeinfo = procmime_scan_file_with_offset(filename, 0);
1690
1691         return mimeinfo;
1692 }
1693
1694 MimeInfo *procmime_scan_queue_file(const gchar *filename)
1695 {
1696         FILE *fp;
1697         MimeInfo *mimeinfo;
1698         gchar buf[BUFFSIZE];
1699         gint offset = 0;
1700
1701         g_return_val_if_fail(filename != NULL, NULL);
1702
1703         /* Open file */
1704         if ((fp = fopen(filename, "rb")) == NULL)
1705                 return NULL;
1706         /* Skip queue header */
1707         while (fgets(buf, sizeof(buf), fp) != NULL)
1708                 if (buf[0] == '\r' || buf[0] == '\n') break;
1709         offset = ftell(fp);
1710         fclose(fp);
1711
1712         mimeinfo = procmime_scan_file_with_offset(filename, offset);
1713
1714         return mimeinfo;
1715 }
1716
1717 typedef enum {
1718     ENC_AS_TOKEN,
1719     ENC_AS_QUOTED_STRING,
1720     ENC_AS_EXTENDED,
1721 } EncodeAs;
1722
1723 typedef struct _ParametersData {
1724         FILE *fp;
1725         guint len;
1726 } ParametersData;
1727
1728 static void write_parameters(gpointer key, gpointer value, gpointer user_data)
1729 {
1730         gchar *param = key;
1731         gchar *val = value, *valpos;
1732         ParametersData *pdata = (ParametersData *)user_data;
1733         GString *buf = g_string_new("");
1734
1735         EncodeAs encas = ENC_AS_TOKEN;
1736
1737         for (valpos = val; *valpos != 0; valpos++) {
1738                 if (!IS_ASCII(*valpos)) {
1739                         encas = ENC_AS_EXTENDED;
1740                         break;
1741                 }
1742             
1743                 /* CTLs */
1744                 if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
1745                         encas = ENC_AS_QUOTED_STRING;
1746                         continue;
1747                 }
1748
1749                 /* tspecials + SPACE */
1750                 switch (*valpos) {
1751                 case ' ':
1752                 case '(': 
1753                 case ')':
1754                 case '<':
1755                 case '>':
1756                 case '@':
1757                 case ',':
1758                 case ';':
1759                 case ':':
1760                 case '\\':
1761                 case '"':
1762                 case '/':
1763                 case '[':
1764                 case ']':
1765                 case '?':
1766                 case '=':
1767                         encas = ENC_AS_QUOTED_STRING;
1768                         continue;
1769                 }
1770         }
1771
1772         switch (encas) {
1773         case ENC_AS_TOKEN:
1774                 g_string_append_printf(buf, "%s=%s", param, val);
1775                 break;
1776
1777         case ENC_AS_QUOTED_STRING:
1778                 g_string_append_printf(buf, "%s=\"%s\"", param, val);
1779                 break;
1780
1781         case ENC_AS_EXTENDED:
1782                 g_string_append_printf(buf, "%s*=%s''", param,
1783                         conv_get_locale_charset_str());
1784                 for (valpos = val; *valpos != '\0'; valpos++) {
1785                         if (IS_ASCII(*valpos) && isalnum(*valpos)) {
1786                                 g_string_append_printf(buf, "%c", *valpos);
1787                         } else {
1788                                 gchar hexstr[3] = "XX";
1789                                 get_hex_str(hexstr, *valpos);
1790                                 g_string_append_printf(buf, "%%%s", hexstr);
1791                         }
1792                 }
1793                 break;
1794         }
1795         
1796         if (buf->str && strlen(buf->str)) {
1797                 if (pdata->len + strlen(buf->str) + 2 > 76) {
1798                         fprintf(pdata->fp, ";\n %s", buf->str);
1799                         pdata->len = strlen(buf->str) + 1;
1800                 } else {
1801                         fprintf(pdata->fp, "; %s", buf->str);
1802                         pdata->len += strlen(buf->str) + 2;
1803                 }
1804         }
1805         g_string_free(buf, TRUE);
1806 }
1807
1808 void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
1809 {
1810         struct TypeTable *type_table;
1811         ParametersData *pdata = g_new0(ParametersData, 1);
1812         debug_print("procmime_write_mime_header\n");
1813         
1814         pdata->fp = fp;
1815
1816         for (type_table = mime_type_table; type_table->str != NULL; type_table++)
1817                 if (mimeinfo->type == type_table->type) {
1818                         gchar *buf = g_strdup_printf(
1819                                 "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
1820                         fprintf(fp, "%s", buf);
1821                         pdata->len = strlen(buf);
1822                         g_free(buf);
1823                         break;
1824                 }
1825         g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
1826         g_free(pdata);
1827
1828         fprintf(fp, "\n");
1829
1830         if (mimeinfo->encoding_type != ENC_UNKNOWN)
1831                 fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
1832
1833         if (mimeinfo->description != NULL)
1834                 fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
1835
1836         if (mimeinfo->id != NULL)
1837                 fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
1838
1839         if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
1840                 ParametersData *pdata = g_new0(ParametersData, 1);
1841                 gchar *buf = NULL;
1842                 if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
1843                         buf = g_strdup("Content-Disposition: inline");
1844                 else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
1845                         buf = g_strdup("Content-Disposition: attachment");
1846                 else
1847                         buf = g_strdup("Content-Disposition: unknown");
1848
1849                 fprintf(fp, "%s", buf);
1850                 pdata->len = strlen(buf);
1851                 g_free(buf);
1852
1853                 pdata->fp = fp;
1854                 g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
1855                 g_free(pdata);
1856                 fprintf(fp, "\n");
1857         }
1858
1859         fprintf(fp, "\n");
1860 }
1861
1862 gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
1863 {
1864         FILE *infp;
1865         GNode *childnode;
1866         MimeInfo *child;
1867         gchar buf[BUFFSIZE];
1868         gboolean skip = FALSE;;
1869
1870         debug_print("procmime_write_message_rfc822\n");
1871
1872         /* write header */
1873         switch (mimeinfo->content) {
1874         case MIMECONTENT_FILE:
1875                 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1876                         FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1877                         return -1;
1878                 }
1879                 fseek(infp, mimeinfo->offset, SEEK_SET);
1880                 while (fgets(buf, sizeof(buf), infp) == buf) {
1881                         if (buf[0] == '\n' && buf[1] == '\0')
1882                                 break;
1883                         if (skip && (buf[0] == ' ' || buf[0] == '\t'))
1884                                 continue;
1885                         if (g_ascii_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
1886                             g_ascii_strncasecmp(buf, "Content-Type:", 13) == 0 ||
1887                             g_ascii_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
1888                             g_ascii_strncasecmp(buf, "Content-Description:", 20) == 0 ||
1889                             g_ascii_strncasecmp(buf, "Content-ID:", 11) == 0 ||
1890                             g_ascii_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
1891                                 skip = TRUE;
1892                                 continue;
1893                         }
1894                         fwrite(buf, sizeof(gchar), strlen(buf), fp);
1895                         skip = FALSE;
1896                 }
1897                 fclose(infp);
1898                 break;
1899
1900         case MIMECONTENT_MEM:
1901                 fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
1902                 break;
1903
1904         default:
1905                 break;
1906         }
1907
1908         childnode = mimeinfo->node->children;
1909         if (childnode == NULL)
1910                 return -1;
1911
1912         child = (MimeInfo *) childnode->data;
1913         fprintf(fp, "Mime-Version: 1.0\n");
1914         procmime_write_mime_header(child, fp);
1915         return procmime_write_mimeinfo(child, fp);
1916 }
1917
1918 gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
1919 {
1920         FILE *infp;
1921         GNode *childnode;
1922         gchar *boundary, *str, *str2;
1923         gchar buf[BUFFSIZE];
1924         gboolean firstboundary;
1925
1926         debug_print("procmime_write_multipart\n");
1927
1928         boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
1929
1930         switch (mimeinfo->content) {
1931         case MIMECONTENT_FILE:
1932                 if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1933                         FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1934                         return -1;
1935                 }
1936                 fseek(infp, mimeinfo->offset, SEEK_SET);
1937                 while (fgets(buf, sizeof(buf), infp) == buf) {
1938                         if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
1939                                 break;
1940                         fwrite(buf, sizeof(gchar), strlen(buf), fp);
1941                 }
1942                 fclose(infp);
1943                 break;
1944
1945         case MIMECONTENT_MEM:
1946                 str = g_strdup(mimeinfo->data.mem);
1947                 if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
1948                     (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
1949                         *(str2 - 2) = '\0';
1950                 fwrite(str, strlen(str), sizeof(gchar), fp);
1951                 g_free(str);
1952                 break;
1953
1954         default:
1955                 break;
1956         }
1957
1958         childnode = mimeinfo->node->children;
1959         firstboundary = TRUE;
1960         while (childnode != NULL) {
1961                 MimeInfo *child = childnode->data;
1962
1963                 if (firstboundary)
1964                         firstboundary = FALSE;
1965                 else
1966                         fprintf(fp, "\n");
1967                 fprintf(fp, "--%s\n", boundary);
1968
1969                 procmime_write_mime_header(child, fp);
1970                 if (procmime_write_mimeinfo(child, fp) < 0)
1971                         return -1;
1972
1973                 childnode = g_node_next_sibling(childnode);
1974         }       
1975         fprintf(fp, "\n--%s--\n", boundary);
1976
1977         return 0;
1978 }
1979
1980 gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
1981 {
1982         FILE *infp;
1983
1984         debug_print("procmime_write_mimeinfo\n");
1985
1986         if (G_NODE_IS_LEAF(mimeinfo->node)) {
1987                 switch (mimeinfo->content) {
1988                 case MIMECONTENT_FILE:
1989                         if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
1990                                 FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
1991                                 return -1;
1992                         }
1993                         copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
1994                         fclose(infp);
1995                         return 0;
1996
1997                 case MIMECONTENT_MEM:
1998                         fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
1999                         return 0;
2000
2001                 default:
2002                         return 0;
2003                 }
2004         } else {
2005                 /* Call writer for mime type */
2006                 switch (mimeinfo->type) {
2007                 case MIMETYPE_MESSAGE:
2008                         if (g_ascii_strcasecmp(mimeinfo->subtype, "rfc822") == 0)
2009                                 return procmime_write_message_rfc822(mimeinfo, fp);
2010                         break;
2011                         
2012                 case MIMETYPE_MULTIPART:
2013                         return procmime_write_multipart(mimeinfo, fp);
2014                         
2015                 default:
2016                         break;
2017                 }
2018
2019                 return -1;
2020         }
2021
2022         return 0;
2023 }