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