* src/procmime.[ch]
[claws.git] / src / procmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
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 <glib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <locale.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include "intl.h"
36 #include "procmime.h"
37 #include "procheader.h"
38 #include "base64.h"
39 #include "quoted-printable.h"
40 #include "uuencode.h"
41 #include "unmime.h"
42 #include "html.h"
43 #include "enriched.h"
44 #include "codeconv.h"
45 #include "utils.h"
46 #include "prefs_common.h"
47
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->type          = MIMETYPE_UNKNOWN;
58         mimeinfo->encoding_type = ENC_UNKNOWN;
59
60         mimeinfo->parameters = g_hash_table_new(g_str_hash, g_str_equal);
61         mimeinfo->node       = g_node_new(mimeinfo);
62         
63         return mimeinfo;
64 }
65
66 static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
67 {
68         g_free(key);
69         g_free(value);
70         
71         return TRUE;
72 }
73
74 static gboolean free_func(GNode *node, gpointer data)
75 {
76         MimeInfo *mimeinfo = (MimeInfo *) node->data;
77
78         g_free(mimeinfo->encoding);
79         g_free(mimeinfo->name);
80         if(mimeinfo->tmpfile)
81                 unlink(mimeinfo->filename);
82         g_free(mimeinfo->filename);
83
84         g_free(mimeinfo->subtype);
85         g_free(mimeinfo->description);
86         g_free(mimeinfo->id);
87
88         g_hash_table_foreach_remove(mimeinfo->parameters, procmime_mimeinfo_parameters_destroy, NULL);
89         g_hash_table_destroy(mimeinfo->parameters);
90
91         if (mimeinfo->privacy)
92                 privacy_free_privacydata(mimeinfo->privacy);
93
94         g_free(mimeinfo);
95
96         return FALSE;
97 }
98
99 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
100 {
101         GNode *node;
102
103         g_return_if_fail(mimeinfo);
104
105         node = mimeinfo->node;
106         g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
107
108         g_node_destroy(node);
109 }
110
111 #if 0 /* UNUSED */
112 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
113 {
114         MimeInfo *child = parent->children;
115
116         if (!child)
117                 parent->children = mimeinfo;
118         else {
119                 while (child->next != NULL)
120                         child = child->next;
121
122                 child->next = mimeinfo;
123         }
124
125         mimeinfo->parent = parent;
126         mimeinfo->level = parent->level + 1;
127
128         return mimeinfo;
129 }
130
131 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
132 {
133         MimeInfo *parent = old->parent;
134         MimeInfo *child;
135
136         g_return_if_fail(parent != NULL);
137         g_return_if_fail(new->next == NULL);
138
139         for (child = parent->children; child && child != old;
140              child = child->next)
141                 ;
142         if (!child) {
143                 g_warning("oops: parent can't find it's own child");
144                 return;
145         }
146         procmime_mimeinfo_free_all(old);
147
148         if (child == parent->children) {
149                 new->next = parent->children->next;
150                 parent->children = new;
151         } else {
152                 new->next = child->next;
153                 child = new;
154         }
155 }
156 #endif
157
158 MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
159 {
160         g_return_val_if_fail(mimeinfo != NULL, NULL);
161
162         if (mimeinfo->node->parent == NULL)
163                 return NULL;
164         return (MimeInfo *) mimeinfo->node->parent->data;
165 }
166
167 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
168 {
169         g_return_val_if_fail(mimeinfo != NULL, NULL);
170         g_return_val_if_fail(mimeinfo->node != NULL, NULL);
171
172         if (mimeinfo->node->children)
173                 return (MimeInfo *) mimeinfo->node->children->data;
174         if (mimeinfo->node->next)
175                 return (MimeInfo *) mimeinfo->node->next->data;
176
177         if (mimeinfo->node->parent == NULL)
178                 return NULL;
179
180         while (mimeinfo->node->parent != NULL) {
181                 mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
182                 if (mimeinfo->node->next)
183                         return (MimeInfo *) mimeinfo->node->next->data;
184         }
185
186         return NULL;
187 }
188
189 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
190 {
191         gchar *filename;
192         MimeInfo *mimeinfo;
193
194         filename = procmsg_get_message_file(msginfo);
195         if(!filename)
196                 return NULL;
197         if (msginfo->folder->stype != F_QUEUE && 
198             msginfo->folder->stype != F_DRAFT)
199                 mimeinfo = procmime_scan_file(filename);
200         else
201                 mimeinfo = procmime_scan_queue_file(filename);
202         g_free(filename);
203
204         return mimeinfo;
205 }
206
207 enum
208 {
209         H_CONTENT_TRANSFER_ENCODING = 0,
210         H_CONTENT_TYPE              = 1,
211         H_CONTENT_DISPOSITION       = 2,
212         H_CONTENT_DESCRIPTION       = 3,
213         H_SUBJECT                   = 4
214 };
215
216 const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
217 {
218         g_return_val_if_fail(mimeinfo != NULL, NULL);
219         g_return_val_if_fail(name != NULL, NULL);
220         
221         return g_hash_table_lookup(mimeinfo->parameters, name);
222 }
223
224 gboolean procmime_decode_content(MimeInfo *mimeinfo)
225 {
226         gchar buf[BUFFSIZE];
227         gint readend;
228         gchar *tmpfilename;
229         gchar *mimetmpdir;
230         FILE *outfp, *infp;
231         struct stat statbuf;
232
233         g_return_val_if_fail(mimeinfo != NULL, FALSE);
234
235         if(mimeinfo->encoding_type == ENC_BINARY)
236                 return TRUE;
237
238         infp = fopen(mimeinfo->filename, "rb");
239         if(!infp) {
240                 perror("fopen");
241                 return FALSE;
242         }
243         fseek(infp, mimeinfo->offset, SEEK_SET);
244
245         mimetmpdir = get_mime_tmp_dir();
246         outfp = get_tmpfile_in_dir(mimetmpdir, &tmpfilename);
247         if (!outfp) {
248                 perror("tmpfile");
249                 return FALSE;
250         }
251
252         readend = mimeinfo->offset + mimeinfo->length;
253
254         if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
255                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
256                         gint len;
257                         len = qp_decode_line(buf);
258                         fwrite(buf, len, 1, outfp);
259                 }
260         } else if (mimeinfo->encoding_type == ENC_BASE64) {
261                 gchar outbuf[BUFFSIZE];
262                 gint len;
263                 Base64Decoder *decoder;
264
265                 decoder = base64_decoder_new();
266                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
267                         len = base64_decoder_decode(decoder, buf, outbuf);
268                         if (len < 0) {
269                                 g_warning("Bad BASE64 content\n");
270                                 break;
271                         }
272                         fwrite(outbuf, sizeof(gchar), len, outfp);
273                 }
274                 base64_decoder_free(decoder);
275         } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
276                 gchar outbuf[BUFFSIZE];
277                 gint len;
278                 gboolean flag = FALSE;
279
280                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
281                         if(!flag && strncmp(buf,"begin ", 6)) continue;
282
283                         if (flag) {
284                                 len = fromuutobits(outbuf, buf);
285                                 if (len <= 0) {
286                                         if (len < 0) 
287                                                 g_warning("Bad UUENCODE content(%d)\n", len);
288                                         break;
289                                 }
290                                 fwrite(outbuf, sizeof(gchar), len, outfp);
291                         } else
292                                 flag = TRUE;
293                 }
294         } else {
295                 while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
296                         fputs(buf, outfp);
297                 }
298         }
299
300         fclose(outfp);
301         fclose(infp);
302
303         stat(tmpfilename, &statbuf);
304         if(mimeinfo->tmpfile)
305                 unlink(mimeinfo->filename);
306         g_free(mimeinfo->filename);
307         mimeinfo->filename = tmpfilename;
308         mimeinfo->tmpfile = TRUE;
309         mimeinfo->offset = 0;
310         mimeinfo->length = statbuf.st_size;
311         mimeinfo->encoding_type = ENC_BINARY;
312
313         return TRUE;
314 }
315
316 gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
317 {
318         FILE *infp, *outfp;
319         gchar buf[BUFFSIZE];
320         gint restlength, readlength;
321
322         g_return_val_if_fail(outfile != NULL, -1);
323         g_return_val_if_fail(mimeinfo != NULL, -1);
324
325         if(mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
326                 return -1;
327
328         if ((infp = fopen(mimeinfo->filename, "rb")) == NULL) {
329                 FILE_OP_ERROR(mimeinfo->filename, "fopen");
330                 return -1;
331         }
332         if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
333                 FILE_OP_ERROR(mimeinfo->filename, "fseek");
334                 fclose(infp);
335                 return -1;
336         }
337         if ((outfp = fopen(outfile, "wb")) == NULL) {
338                 FILE_OP_ERROR(outfile, "fopen");
339                 fclose(infp);
340                 return -1;
341         }
342
343         restlength = mimeinfo->length;
344
345         while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
346                 fwrite(buf, 1, readlength, outfp);
347                 restlength -= readlength;
348         }
349
350         fclose(infp);
351         if (fclose(outfp) == EOF) {
352                 FILE_OP_ERROR(outfile, "fclose");
353                 unlink(outfile);
354                 return -1;
355         }
356
357         return 0;
358 }
359
360 struct ContentRenderer {
361         char * content_type;
362         char * renderer;
363 };
364
365 static GList * renderer_list = NULL;
366
367 static struct ContentRenderer *
368 content_renderer_new(char * content_type, char * renderer)
369 {
370         struct ContentRenderer * cr;
371
372         cr = g_new(struct ContentRenderer, 1);
373         if (cr == NULL)
374                 return NULL;
375
376         cr->content_type = g_strdup(content_type);
377         cr->renderer = g_strdup(renderer);
378
379         return cr;
380 }
381
382 static void content_renderer_free(struct ContentRenderer * cr)
383 {
384         g_free(cr->content_type);
385         g_free(cr->renderer);
386         g_free(cr);
387 }
388
389 void renderer_read_config(void)
390 {
391         gchar buf[BUFFSIZE];
392         FILE * f;
393         gchar * rcpath;
394
395         g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
396         renderer_list = NULL;
397
398         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
399         f = fopen(rcpath, "rb");
400         g_free(rcpath);
401         
402         if (f == NULL)
403                 return;
404
405         while (fgets(buf, BUFFSIZE, f)) {
406                 char * p;
407                 struct ContentRenderer * cr;
408
409                 strretchomp(buf);
410                 p = strchr(buf, ' ');
411                 if (p == NULL)
412                         continue;
413                 * p = 0;
414
415                 cr = content_renderer_new(buf, p + 1);
416                 if (cr == NULL)
417                         continue;
418
419                 renderer_list = g_list_append(renderer_list, cr);
420         }
421
422         fclose(f);
423 }
424
425 void renderer_write_config(void)
426 {
427         gchar * rcpath;
428         PrefFile *pfile;
429         GList * cur;
430
431         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
432         
433         if ((pfile = prefs_write_open(rcpath)) == NULL) {
434                 g_warning("failed to write configuration to file\n");
435                 g_free(rcpath);
436                 return;
437         }
438
439         g_free(rcpath);
440
441         for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
442                 struct ContentRenderer * renderer;
443                 renderer = cur->data;
444                 fprintf(pfile->fp, "%s %s\n", renderer->content_type,
445                         renderer->renderer);
446         }
447
448         if (prefs_file_close(pfile) < 0) {
449                 g_warning("failed to write configuration to file\n");
450                 return;
451         }
452 }
453
454 FILE *procmime_get_text_content(MimeInfo *mimeinfo)
455 {
456         FILE *tmpfp, *outfp;
457         const gchar *src_codeset;
458         gboolean conv_fail = FALSE;
459         gchar buf[BUFFSIZE];
460         gchar *str;
461         struct ContentRenderer * renderer;
462         GList * cur;
463         gchar *tmpfile, *content_type;
464     
465         g_return_val_if_fail(mimeinfo != NULL, NULL);
466
467         if (!procmime_decode_content(mimeinfo))
468                 return NULL;
469
470         tmpfile = procmime_get_tmp_file_name(mimeinfo);
471         if (tmpfile == NULL)
472                 return NULL;
473
474         if (procmime_get_part(tmpfile, mimeinfo) < 0) {
475                 g_free(tmpfile);
476                 return NULL;
477         }
478
479         tmpfp = fopen(tmpfile, "rb");
480         if (tmpfp == NULL) {
481                 g_free(tmpfile);
482                 return NULL;
483         }
484
485         if ((outfp = my_tmpfile()) == NULL) {
486                 perror("tmpfile");
487                 fclose(tmpfp);
488                 g_free(tmpfile);
489                 return NULL;
490         }
491
492         src_codeset = prefs_common.force_charset
493                 ? prefs_common.force_charset : 
494                 procmime_mimeinfo_get_parameter(mimeinfo, "charset");
495
496         renderer = NULL;
497
498         content_type = g_strdup_printf("%s/%s", procmime_get_type_str(mimeinfo->type),
499                 mimeinfo->subtype);
500         for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
501                 struct ContentRenderer * cr;
502
503                 cr = cur->data;
504                 if (g_strcasecmp(cr->content_type, content_type) == 0) {
505                         renderer = cr;
506                         break;
507                 }
508         }
509         g_free(content_type);
510
511         if (renderer != NULL) {
512                 FILE * p;
513                 int oldout;
514                 
515                 oldout = dup(1);
516                 
517                 dup2(fileno(outfp), 1);
518                 
519                 p = popen(renderer->renderer, "w");
520                 if (p != NULL) {
521                         size_t count;
522                         
523                         while ((count =
524                                 fread(buf, sizeof(char), sizeof(buf),
525                                       tmpfp)) > 0)
526                                 fwrite(buf, sizeof(char), count, p);
527                         pclose(p);
528                 }
529                 
530                 dup2(oldout, 1);
531         } else if (mimeinfo->type == MIMETYPE_TEXT && !g_strcasecmp(mimeinfo->subtype, "html")) {
532                 HTMLParser *parser;
533                 CodeConverter *conv;
534
535                 conv = conv_code_converter_new(src_codeset);
536                 parser = html_parser_new(tmpfp, conv);
537                 while ((str = html_parse(parser)) != NULL) {
538                         fputs(str, outfp);
539                 }
540                 html_parser_destroy(parser);
541                 conv_code_converter_destroy(conv);
542         } else if (mimeinfo->type == MIMETYPE_TEXT && !g_strcasecmp(mimeinfo->subtype, "enriched")) {
543                 ERTFParser *parser;
544                 CodeConverter *conv;
545
546                 conv = conv_code_converter_new(src_codeset);
547                 parser = ertf_parser_new(tmpfp, conv);
548                 while ((str = ertf_parse(parser)) != NULL) {
549                         fputs(str, outfp);
550                 }
551                 ertf_parser_destroy(parser);
552                 conv_code_converter_destroy(conv);
553         } else if (mimeinfo->type == MIMETYPE_TEXT) {
554                 while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
555                         str = conv_codeset_strdup(buf, src_codeset, NULL);
556                         if (str) {
557                                 fputs(str, outfp);
558                                 g_free(str);
559                         } else {
560                                 conv_fail = TRUE;
561                                 fputs(buf, outfp);
562                         }
563                 }
564         }
565
566         if (conv_fail)
567                 g_warning("procmime_get_text_content(): Code conversion failed.\n");
568
569         fclose(tmpfp);
570         rewind(outfp);
571         unlink(tmpfile);
572         g_free(tmpfile);
573
574         return outfp;
575 }
576
577 /* search the first text part of (multipart) MIME message,
578    decode, convert it and output to outfp. */
579 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
580 {
581         FILE *outfp = NULL;
582         MimeInfo *mimeinfo, *partinfo;
583
584         g_return_val_if_fail(msginfo != NULL, NULL);
585
586         mimeinfo = procmime_scan_message(msginfo);
587         if (!mimeinfo) return NULL;
588
589         partinfo = mimeinfo;
590         while (partinfo && partinfo->type != MIMETYPE_TEXT)
591                 partinfo = procmime_mimeinfo_next(partinfo);
592
593         if (partinfo)
594                 outfp = procmime_get_text_content(partinfo);
595
596         procmime_mimeinfo_free_all(mimeinfo);
597
598         return outfp;
599 }
600
601 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
602                                    const gchar *str, gboolean case_sens)
603 {
604         FILE *outfp;
605         gchar buf[BUFFSIZE];
606         gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
607
608         g_return_val_if_fail(mimeinfo != NULL, FALSE);
609         g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
610         g_return_val_if_fail(str != NULL, FALSE);
611
612         outfp = procmime_get_text_content(mimeinfo);
613
614         if (!outfp)
615                 return FALSE;
616
617         if (case_sens)
618                 StrFindFunc = strstr;
619         else
620                 StrFindFunc = strcasestr;
621
622         while (fgets(buf, sizeof(buf), outfp) != NULL) {
623                 if (StrFindFunc(buf, str) != NULL) {
624                         fclose(outfp);
625                         return TRUE;
626                 }
627         }
628
629         fclose(outfp);
630
631         return FALSE;
632 }
633
634 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
635                               gboolean case_sens)
636 {
637         MimeInfo *mimeinfo;
638         MimeInfo *partinfo;
639         gchar *filename;
640         gboolean found = FALSE;
641
642         g_return_val_if_fail(msginfo != NULL, FALSE);
643         g_return_val_if_fail(str != NULL, FALSE);
644
645         filename = procmsg_get_message_file(msginfo);
646         if (!filename) return FALSE;
647         mimeinfo = procmime_scan_message(msginfo);
648
649         for (partinfo = mimeinfo; partinfo != NULL;
650              partinfo = procmime_mimeinfo_next(partinfo)) {
651                 if (partinfo->type == MIMETYPE_TEXT) {
652                         if (procmime_find_string_part
653                                 (partinfo, filename, str, case_sens) == TRUE) {
654                                 found = TRUE;
655                                 break;
656                         }
657                 }
658         }
659
660         procmime_mimeinfo_free_all(mimeinfo);
661         g_free(filename);
662
663         return found;
664 }
665
666 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
667 {
668         static guint32 id = 0;
669         gchar *base;
670         gchar *filename;
671         gchar f_prefix[10];
672
673         g_return_val_if_fail(mimeinfo != NULL, NULL);
674
675         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
676
677         if ((mimeinfo->type == MIMETYPE_TEXT) && !g_strcasecmp(mimeinfo->subtype, "html"))
678                 base = "mimetmp.html";
679         else {
680                 base = mimeinfo->name ? mimeinfo->name : "mimetmp";
681                 base = g_basename(base);
682                 if (*base == '\0') base = "mimetmp";
683                 Xstrdup_a(base, base, return NULL);
684                 subst_for_filename(base);
685         }
686
687         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
688                                f_prefix, base, NULL);
689
690         return filename;
691 }
692
693 static GList *mime_type_list = NULL;
694
695 gchar *procmime_get_mime_type(const gchar *filename)
696 {
697         static GHashTable *mime_type_table = NULL;
698         MimeType *mime_type;
699         const gchar *p;
700         gchar *ext;
701
702         if (!mime_type_table) {
703                 mime_type_table = procmime_get_mime_type_table();
704                 if (!mime_type_table) return NULL;
705         }
706
707         filename = g_basename(filename);
708         p = strrchr(filename, '.');
709         if (!p) return NULL;
710
711         Xstrdup_a(ext, p + 1, return NULL);
712         g_strdown(ext);
713         mime_type = g_hash_table_lookup(mime_type_table, ext);
714         if (mime_type) {
715                 gchar *str;
716
717                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
718                                   NULL);
719                 return str;
720         }
721
722         return NULL;
723 }
724
725 static guint procmime_str_hash(gconstpointer gptr)
726 {
727         guint hash_result = 0;
728         const char *str;
729
730         for (str = gptr; str && *str; str++) {
731                 if (isupper(*str)) hash_result += (*str + ' ');
732                 else hash_result += *str;
733         }
734
735         return hash_result;
736 }
737
738 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
739 {
740         const char *str1 = gptr1;
741         const char *str2 = gptr2;
742
743         return !strcasecmp(str1, str2);
744 }
745
746 static GHashTable *procmime_get_mime_type_table(void)
747 {
748         GHashTable *table = NULL;
749         GList *cur;
750         MimeType *mime_type;
751         gchar **exts;
752
753         if (!mime_type_list) {
754                 mime_type_list = procmime_get_mime_type_list();
755                 if (!mime_type_list) return NULL;
756         }
757
758         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
759
760         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
761                 gint i;
762                 gchar *key;
763
764                 mime_type = (MimeType *)cur->data;
765
766                 if (!mime_type->extension) continue;
767
768                 exts = g_strsplit(mime_type->extension, " ", 16);
769                 for (i = 0; exts[i] != NULL; i++) {
770                         /* make the key case insensitive */
771                         g_strdown(exts[i]);
772                         /* use previously dup'd key on overwriting */
773                         if (g_hash_table_lookup(table, exts[i]))
774                                 key = exts[i];
775                         else
776                                 key = g_strdup(exts[i]);
777                         g_hash_table_insert(table, key, mime_type);
778                 }
779                 g_strfreev(exts);
780         }
781
782         return table;
783 }
784
785 GList *procmime_get_mime_type_list(void)
786 {
787         GList *list = NULL;
788         FILE *fp;
789         gchar buf[BUFFSIZE];
790         gchar *p, *delim;
791         MimeType *mime_type;
792
793         if (mime_type_list) 
794                 return mime_type_list;
795
796         if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
797                 if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
798                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
799                         return NULL;
800                 }
801         }
802
803         while (fgets(buf, sizeof(buf), fp) != NULL) {
804                 p = strchr(buf, '#');
805                 if (p) *p = '\0';
806                 g_strstrip(buf);
807
808                 p = buf;
809                 while (*p && !isspace(*p)) p++;
810                 if (*p) {
811                         *p = '\0';
812                         p++;
813                 }
814                 delim = strchr(buf, '/');
815                 if (delim == NULL) continue;
816                 *delim = '\0';
817
818                 mime_type = g_new(MimeType, 1);
819                 mime_type->type = g_strdup(buf);
820                 mime_type->sub_type = g_strdup(delim + 1);
821
822                 while (*p && isspace(*p)) p++;
823                 if (*p)
824                         mime_type->extension = g_strdup(p);
825                 else
826                         mime_type->extension = NULL;
827
828                 list = g_list_append(list, mime_type);
829         }
830
831         fclose(fp);
832
833         if (!list)
834                 g_warning("Can't read mime.types\n");
835
836         return list;
837 }
838
839 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
840 {
841         if (!charset)
842                 return ENC_8BIT;
843         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
844                  !strcasecmp(charset, "US-ASCII"))
845                 return ENC_7BIT;
846         else if (!strcasecmp(charset, "ISO-8859-5") ||
847                  !strncasecmp(charset, "KOI8-", 5) ||
848                  !strcasecmp(charset, "Windows-1251"))
849                 return ENC_8BIT;
850         else if (!strncasecmp(charset, "ISO-8859-", 9))
851                 return ENC_QUOTED_PRINTABLE;
852         else
853                 return ENC_8BIT;
854 }
855
856 EncodingType procmime_get_encoding_for_file(const gchar *file)
857 {
858         FILE *fp;
859         guchar buf[BUFSIZ];
860         size_t len;
861
862         if ((fp = fopen(file, "rb")) == NULL) {
863                 FILE_OP_ERROR(file, "fopen");
864                 return ENC_UNKNOWN;
865         }
866
867         while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
868                 guchar *p;
869                 gint i;
870
871                 for (p = buf, i = 0; i < len; p++, i++) {
872                         if (*p & 0x80) {
873                                 fclose(fp);
874                                 return ENC_BASE64;
875                         }
876                 }
877         }
878
879         fclose(fp);
880         return ENC_7BIT;
881 }
882
883 struct EncodingTable 
884 {
885         gchar *str;
886         EncodingType enc_type;
887 };
888
889 struct EncodingTable encoding_table[] = {
890         {"7bit", ENC_7BIT},
891         {"8bit", ENC_8BIT},
892         {"binary", ENC_BINARY},
893         {"quoted-printable", ENC_QUOTED_PRINTABLE},
894         {"base64", ENC_BASE64},
895         {"x-uuencode", ENC_UNKNOWN},
896         {NULL, ENC_UNKNOWN},
897 };
898
899 const gchar *procmime_get_encoding_str(EncodingType encoding)
900 {
901         struct EncodingTable *enc_table;
902         
903         for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
904                 if (enc_table->enc_type == encoding)
905                         return enc_table->str;
906         }
907         return NULL;
908 }
909
910 /* --- NEW MIME STUFF --- */
911 struct TypeTable
912 {
913         gchar *str;
914         MimeMediaType type;
915 };
916
917 static struct TypeTable mime_type_table[] = {
918         {"text", MIMETYPE_TEXT},
919         {"image", MIMETYPE_IMAGE},
920         {"audio", MIMETYPE_AUDIO},
921         {"video", MIMETYPE_VIDEO},
922         {"application", MIMETYPE_APPLICATION},
923         {"message", MIMETYPE_MESSAGE},
924         {"multipart", MIMETYPE_MULTIPART},
925         {NULL, 0},
926 };
927
928 const gchar *procmime_get_type_str(MimeMediaType type)
929 {
930         struct TypeTable *type_table;
931         
932         for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
933                 if (type_table->type == type)
934                         return type_table->str;
935         }
936         return NULL;
937 }
938
939 void procmime_parse_mimepart(MimeInfo *parent,
940                              gchar *content_type,
941                              gchar *content_encoding,
942                              gchar *content_description,
943                              gchar *content_id,
944                              gchar *content_disposition,
945                              const gchar *filename,
946                              guint offset,
947                              guint length);
948
949 void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
950 {
951         HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
952                                 {"Content-Transfer-Encoding:",
953                                                    NULL, FALSE},
954                                 {"Content-Description:",
955                                                    NULL, TRUE},
956                                 {"Content-ID:",
957                                                    NULL, TRUE},
958                                 {"Content-Disposition:",
959                                                    NULL, TRUE},
960                                 {NULL,             NULL, FALSE}};
961         guint content_start, i;
962         FILE *fp;
963
964         if(mimeinfo->encoding_type != ENC_BINARY && 
965            mimeinfo->encoding_type != ENC_7BIT && 
966            mimeinfo->encoding_type != ENC_8BIT)
967                 procmime_decode_content(mimeinfo);
968
969         fp = fopen(mimeinfo->filename, "rb");
970         fseek(fp, mimeinfo->offset, SEEK_SET);
971         procheader_get_header_fields(fp, hentry);
972         content_start = ftell(fp);
973         fclose(fp);
974
975         procmime_parse_mimepart(mimeinfo,
976                                 hentry[0].body, hentry[1].body,
977                                 hentry[2].body, hentry[3].body, 
978                                 hentry[4].body, 
979                                 mimeinfo->filename, content_start,
980                                 mimeinfo->length - (content_start - mimeinfo->offset));
981         for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
982                 g_free(hentry[i].body);
983                 hentry[i].body = NULL;
984         }
985 }
986
987 void procmime_parse_multipart(MimeInfo *mimeinfo)
988 {
989         HeaderEntry hentry[] = {{"Content-Type:",  NULL, TRUE},
990                                 {"Content-Transfer-Encoding:",
991                                                    NULL, FALSE},
992                                 {"Content-Description:",
993                                                    NULL, TRUE},
994                                 {"Content-ID:",
995                                                    NULL, TRUE},
996                                 {"Content-Disposition:",
997                                                    NULL, TRUE},
998                                 {NULL,             NULL, FALSE}};
999         gchar *p;
1000         gchar *boundary;
1001         gint boundary_len = 0, lastoffset = -1, i;
1002         gchar buf[BUFFSIZE];
1003         FILE *fp;
1004
1005         boundary = g_hash_table_lookup(mimeinfo->parameters, "boundary");
1006         if (!boundary)
1007                 return;
1008         boundary_len = strlen(boundary);
1009
1010         if (mimeinfo->encoding_type != ENC_BINARY && 
1011             mimeinfo->encoding_type != ENC_7BIT && 
1012             mimeinfo->encoding_type != ENC_8BIT)
1013                 procmime_decode_content(mimeinfo);
1014
1015         fp = fopen(mimeinfo->filename, "rb");
1016         fseek(fp, mimeinfo->offset, SEEK_SET);
1017         while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
1018                 if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
1019                         break;
1020
1021                 if (IS_BOUNDARY(buf, boundary, boundary_len)) {
1022                         if (lastoffset != -1) {
1023                                 procmime_parse_mimepart(mimeinfo,
1024                                                         hentry[0].body, hentry[1].body,
1025                                                         hentry[2].body, hentry[3].body, 
1026                                                         hentry[4].body, 
1027                                                         mimeinfo->filename, lastoffset,
1028                                                         (ftell(fp) - strlen(buf)) - lastoffset);
1029                         }
1030                         
1031                         if (buf[2 + boundary_len]     == '-' &&
1032                             buf[2 + boundary_len + 1] == '-')
1033                                 break;
1034
1035                         for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
1036                                 g_free(hentry[i].body);
1037                                 hentry[i].body = NULL;
1038                         }
1039                         procheader_get_header_fields(fp, hentry);
1040                         lastoffset = ftell(fp);
1041                 }
1042         }
1043         for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
1044                 g_free(hentry[i].body);
1045                 hentry[i].body = NULL;
1046         }
1047         fclose(fp);
1048 }
1049
1050 static void add_to_mimeinfo_parameters(gchar **parts, MimeInfo *mimeinfo)
1051 {
1052         gchar **strarray;
1053
1054         for (strarray = parts; *strarray != NULL; strarray++) {
1055                 gchar **parameters_parts;
1056
1057                 parameters_parts = g_strsplit(*strarray, "=", 1);
1058                 if ((parameters_parts[0] != NULL) && (parameters_parts[1] != NULL)) {
1059                         gchar *firstspace;
1060
1061                         g_strstrip(parameters_parts[0]);
1062                         g_strstrip(parameters_parts[1]);
1063                         g_strdown(parameters_parts[0]);
1064                         if(parameters_parts[1][0] == '"')
1065                                 extract_quote(parameters_parts[1], '"');
1066                         else if ((firstspace = strchr(parameters_parts[1], ' ')) != NULL)
1067                                 *firstspace = '\0';
1068                         if(g_hash_table_lookup(mimeinfo->parameters,
1069                                                parameters_parts[0]) == NULL)
1070                                 g_hash_table_insert(mimeinfo->parameters,
1071                                                     g_strdup(parameters_parts[0]),
1072                                                     g_strdup(parameters_parts[1]));
1073                 }
1074                 g_strfreev(parameters_parts);
1075         }
1076 }       
1077
1078 static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
1079 {
1080         gchar **content_type_parts;
1081         gchar **strarray;
1082         gchar *str;
1083         struct TypeTable *typetablearray;
1084         
1085         g_return_if_fail(content_type != NULL);
1086         g_return_if_fail(mimeinfo != NULL);
1087         
1088         /* Split content type into parts and remove trailing
1089            and leading whitespaces from all strings */
1090         content_type_parts = g_strsplit(content_type, ";", 0);
1091         for (strarray = content_type_parts; *strarray != NULL; strarray++) {
1092                 g_strstrip(*strarray);
1093         }
1094
1095         /* Get mimeinfo->type and mimeinfo->subtype */
1096         mimeinfo->type = MIMETYPE_UNKNOWN;
1097         str = content_type_parts[0];
1098         if (str == NULL) {
1099                 g_strfreev(content_type_parts);
1100                 return;
1101         }
1102         for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++) {
1103                 if (g_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0 &&
1104                     str[strlen(typetablearray->str)] == '/') {
1105                         mimeinfo->type = typetablearray->type;
1106                         mimeinfo->subtype = g_strdup(str + strlen(typetablearray->str) + 1);
1107                         break;
1108                 }
1109         }
1110
1111         /* Get mimeinfo->parmeters */
1112         add_to_mimeinfo_parameters(&content_type_parts[1], mimeinfo);
1113         g_strfreev(content_type_parts);
1114 }
1115
1116 static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
1117 {
1118         gchar **content_disp_parts;
1119         gchar **strarray;
1120         gchar *str;
1121
1122         g_return_if_fail(content_disposition != NULL);
1123         g_return_if_fail(mimeinfo != NULL);
1124
1125         /* Split into parts and remove trailing
1126            and leading whitespaces from all strings */
1127         content_disp_parts = g_strsplit(content_disposition, ";", 0);
1128         for (strarray = content_disp_parts; *strarray != NULL; strarray++) {
1129                 g_strstrip(*strarray);
1130         }
1131         /* Get mimeinfo->disposition */
1132         str = content_disp_parts[0];
1133         if (str == NULL) {
1134                 g_strfreev(content_disp_parts);
1135                 return;
1136         }
1137         if (!g_strcasecmp(str, "inline")) 
1138                 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1139         else if (!g_strcasecmp(str, "attachment"))
1140                 mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
1141         else
1142                 mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
1143         
1144         add_to_mimeinfo_parameters(&content_disp_parts[1], mimeinfo);
1145         g_strfreev(content_disp_parts);
1146 }
1147
1148
1149 static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
1150 {
1151         struct EncodingTable *enc_table;
1152         
1153         for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
1154                 if (g_strcasecmp(enc_table->str, content_encoding) == 0) {
1155                         mimeinfo->encoding_type = enc_table->enc_type;
1156                         return;
1157                 }
1158         }
1159         mimeinfo->encoding_type = ENC_UNKNOWN;
1160         return;
1161 }
1162
1163 void procmime_parse_mimepart(MimeInfo *parent,
1164                              gchar *content_type,
1165                              gchar *content_encoding,
1166                              gchar *content_description,
1167                              gchar *content_id,
1168                              gchar *content_disposition,
1169                              const gchar *filename,
1170                              guint offset,
1171                              guint length)
1172 {
1173         MimeInfo *mimeinfo;
1174
1175         /* Create MimeInfo */
1176         mimeinfo = procmime_mimeinfo_new();
1177         if (parent != NULL)
1178                 g_node_append(parent->node, mimeinfo->node);
1179         mimeinfo->filename = g_strdup(filename);
1180         mimeinfo->offset = offset;
1181         mimeinfo->length = length;
1182
1183         if (content_type != NULL) {
1184                 procmime_parse_content_type(content_type, mimeinfo);
1185         } else {
1186                 mimeinfo->type = MIMETYPE_TEXT;
1187                 mimeinfo->subtype = g_strdup("plain");
1188                 g_hash_table_insert(mimeinfo->parameters, g_strdup("charset"), g_strdup("us-ascii"));
1189         }
1190
1191         if (content_encoding != NULL) {
1192                 procmime_parse_content_encoding(content_encoding, mimeinfo);
1193         } else {
1194                 mimeinfo->encoding_type = ENC_7BIT;
1195         }
1196
1197         if (content_description != NULL)
1198                 mimeinfo->description = g_strdup(content_description);
1199         else
1200                 mimeinfo->description = NULL;
1201
1202         if (content_id != NULL)
1203                 mimeinfo->id = g_strdup(content_id);
1204         else
1205                 mimeinfo->id = NULL;
1206
1207         if (content_disposition != NULL) 
1208                 procmime_parse_content_disposition(content_disposition, mimeinfo);
1209         else
1210                 mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
1211
1212         /* Call parser for mime type */
1213         switch (mimeinfo->type) {
1214                 case MIMETYPE_MESSAGE:
1215                         if (g_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
1216                                 procmime_parse_message_rfc822(mimeinfo);
1217                         }
1218                         break;
1219                         
1220                 case MIMETYPE_MULTIPART:
1221                         procmime_parse_multipart(mimeinfo);
1222                         break;
1223                         
1224                 default:
1225                         break;
1226         }
1227 }
1228
1229 static gchar *typenames[] = {
1230     "text",
1231     "image",
1232     "audio",
1233     "video",
1234     "application",
1235     "message",
1236     "multipart",
1237     "unknown",
1238 };
1239
1240 static gboolean output_func(GNode *node, gpointer data)
1241 {
1242         guint i, depth;
1243         MimeInfo *mimeinfo = (MimeInfo *) node->data;
1244
1245         depth = g_node_depth(node);
1246         for(i = 0; i < depth; i++)
1247                 printf("    ");
1248         printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
1249
1250         return FALSE;
1251 }
1252
1253 static void output_mime_structure(MimeInfo *mimeinfo, int indent)
1254 {
1255         g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
1256 }
1257
1258 static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
1259 {
1260         MimeInfo *mimeinfo;
1261         struct stat buf;
1262
1263         stat(filename, &buf);
1264
1265         mimeinfo = procmime_mimeinfo_new();
1266         mimeinfo->encoding_type = ENC_BINARY;
1267         mimeinfo->type = MIMETYPE_MESSAGE;
1268         mimeinfo->subtype = g_strdup("rfc822");
1269         mimeinfo->filename = g_strdup(filename);
1270         mimeinfo->offset = offset;
1271         mimeinfo->length = buf.st_size - offset;
1272
1273         procmime_parse_message_rfc822(mimeinfo);
1274         if (debug_get_mode())
1275                 output_mime_structure(mimeinfo, 0);
1276
1277         return mimeinfo;
1278 }
1279
1280 MimeInfo *procmime_scan_file(gchar *filename)
1281 {
1282         MimeInfo *mimeinfo;
1283
1284         g_return_val_if_fail(filename != NULL, NULL);
1285
1286         mimeinfo = procmime_scan_file_with_offset(filename, 0);
1287
1288         return mimeinfo;
1289 }
1290
1291 MimeInfo *procmime_scan_queue_file(gchar *filename)
1292 {
1293         FILE *fp;
1294         MimeInfo *mimeinfo;
1295         gchar buf[BUFFSIZE];
1296         gint offset = 0;
1297
1298         g_return_val_if_fail(filename != NULL, NULL);
1299
1300         /* Open file */
1301         if((fp = fopen(filename, "rb")) == NULL)
1302                 return NULL;
1303         /* Skip queue header */
1304         while (fgets(buf, sizeof(buf), fp) != NULL)
1305                 if (buf[0] == '\r' || buf[0] == '\n') break;
1306         offset = ftell(fp);
1307         fclose(fp);
1308
1309         mimeinfo = procmime_scan_file_with_offset(filename, offset);
1310
1311         return mimeinfo;
1312 }