sync with sylpheed 0.6.2cvs3
[claws.git] / src / procmime.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 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
32 #include "intl.h"
33 #include "procmime.h"
34 #include "procheader.h"
35 #include "base64.h"
36 #include "uuencode.h"
37 #include "unmime.h"
38 #include "codeconv.h"
39 #include "utils.h"
40 #include "prefs_common.h"
41
42 #if USE_GPGME
43 #  include "rfc2015.h"
44 #endif
45
46 static GHashTable *procmime_get_mime_type_table (void);
47
48 MimeInfo *procmime_mimeinfo_new(void)
49 {
50         MimeInfo *mimeinfo;
51
52         mimeinfo = g_new0(MimeInfo, 1);
53         mimeinfo->mime_type     = MIME_UNKNOWN;
54         mimeinfo->encoding_type = ENC_UNKNOWN;
55
56         return mimeinfo;
57 }
58
59 void procmime_mimeinfo_free(MimeInfo *mimeinfo)
60 {
61         if (!mimeinfo) return;
62
63         g_free(mimeinfo->encoding);
64         g_free(mimeinfo->content_type);
65         g_free(mimeinfo->charset);
66         g_free(mimeinfo->name);
67         g_free(mimeinfo->boundary);
68         g_free(mimeinfo->content_disposition);
69         g_free(mimeinfo->filename);
70 #if USE_GPGME
71         g_free(mimeinfo->plaintextfile);
72         g_free(mimeinfo->sigstatus);
73         g_free(mimeinfo->sigstatus_full);
74 #endif
75
76         procmime_mimeinfo_free(mimeinfo->sub);
77
78         g_free(mimeinfo);
79 }
80
81 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
82 {
83         while (mimeinfo != NULL) {
84                 MimeInfo *next;
85
86                 g_free(mimeinfo->encoding);
87                 g_free(mimeinfo->content_type);
88                 g_free(mimeinfo->charset);
89                 g_free(mimeinfo->name);
90                 g_free(mimeinfo->boundary);
91                 g_free(mimeinfo->content_disposition);
92                 g_free(mimeinfo->filename);
93 #if USE_GPGME
94                 g_free(mimeinfo->plaintextfile);
95                 g_free(mimeinfo->sigstatus);
96                 g_free(mimeinfo->sigstatus_full);
97 #endif
98
99                 procmime_mimeinfo_free_all(mimeinfo->sub);
100                 procmime_mimeinfo_free_all(mimeinfo->children);
101 #if USE_GPGME
102                 procmime_mimeinfo_free_all(mimeinfo->plaintext);
103 #endif
104
105                 next = mimeinfo->next;
106                 g_free(mimeinfo);
107                 mimeinfo = next;
108         }
109 }
110
111 MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
112 {
113         MimeInfo *child = parent->children;
114
115         if (!child)
116                 parent->children = mimeinfo;
117         else {
118                 while (child->next != NULL)
119                         child = child->next;
120
121                 child->next = mimeinfo;
122         }
123
124         mimeinfo->parent = parent;
125         mimeinfo->level = parent->level + 1;
126
127         return mimeinfo;
128 }
129
130 void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
131 {
132         MimeInfo *parent = old->parent;
133         MimeInfo *child;
134
135         if (!parent) {
136                 g_warning("oops: Not top message");
137                 return;
138         }
139         if (new->next) {
140                 g_message("oops: new child should not have a sibling");
141                 return;
142         }
143
144         for (child = parent->children; child && child != old;
145              child = child->next)
146                 ;
147         if (!child) {
148                 g_warning("oops: parent can't find it's own child");
149                 return;
150         }
151         procmime_mimeinfo_free_all(old);
152
153         if (child == parent->children) {
154                 new->next = parent->children->next;
155                 parent->children = new;
156         } else {
157                 new->next = child->next;
158                 child = new;
159         }
160 }
161
162 MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
163 {
164         if (!mimeinfo) return NULL;
165
166         if (mimeinfo->children)
167                 return mimeinfo->children;
168         if (mimeinfo->sub)
169                 return mimeinfo->sub;
170         if (mimeinfo->next)
171                 return mimeinfo->next;
172
173         for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
174              mimeinfo = mimeinfo->parent) {
175                 if (mimeinfo->next)
176                         return mimeinfo->next;
177                 if (mimeinfo->main) {
178                         mimeinfo = mimeinfo->main;
179                         if (mimeinfo->next)
180                                 return mimeinfo->next;
181                 }
182         }
183
184         return NULL;
185 }
186
187 MimeInfo *procmime_scan_message(MsgInfo *msginfo)
188 {
189         FILE *fp;
190         MimeInfo *mimeinfo;
191
192         g_return_val_if_fail(msginfo != NULL, NULL);
193
194         if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
195         mimeinfo = procmime_scan_mime_header(fp);
196
197         if (mimeinfo) {
198                 if (mimeinfo->mime_type != MIME_MULTIPART) {
199                         if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
200                                 perror("fseek");
201                 }
202                 if (mimeinfo->mime_type != MIME_TEXT)
203                         procmime_scan_multipart_message(mimeinfo, fp);
204         }
205
206 #if USE_GPGME
207         if (prefs_common.auto_check_signatures)
208                 rfc2015_check_signature(mimeinfo, fp);
209 #endif
210         fclose(fp);
211
212         return mimeinfo;
213 }
214
215 void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
216 {
217         gchar *p;
218         gchar *boundary;
219         gint boundary_len = 0;
220         gchar buf[BUFFSIZE];
221         glong fpos, prev_fpos;
222         gint npart;
223
224         g_return_if_fail(mimeinfo != NULL);
225         g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
226
227         if (mimeinfo->mime_type == MIME_MULTIPART) {
228                 g_return_if_fail(mimeinfo->boundary != NULL);
229                 g_return_if_fail(mimeinfo->sub == NULL);
230         }
231         g_return_if_fail(fp != NULL);
232
233         boundary = mimeinfo->boundary;
234
235         if (boundary) {
236                 boundary_len = strlen(boundary);
237
238                 /* look for first boundary */
239                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
240                         if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
241                 if (!p) return;
242         }
243
244         if ((fpos = ftell(fp)) < 0) {
245                 perror("ftell");
246                 return;
247         }
248
249         for (npart = 0;; npart++) {
250                 MimeInfo *partinfo;
251                 gboolean eom = FALSE;
252
253                 prev_fpos = fpos;
254
255                 partinfo = procmime_scan_mime_header(fp);
256                 if (!partinfo) break;
257                 procmime_mimeinfo_insert(mimeinfo, partinfo);
258
259                 if (partinfo->mime_type == MIME_MULTIPART) {
260                         if (partinfo->level < 8)
261                                 procmime_scan_multipart_message(partinfo, fp);
262                 } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
263                         MimeInfo *sub;
264
265                         partinfo->sub = sub = procmime_scan_mime_header(fp);
266                         if (!sub) break;
267                         sub->level = partinfo->level + 1;
268                         sub->parent = partinfo->parent;
269                         sub->main = partinfo;
270
271                         if (sub->mime_type == MIME_MULTIPART) {
272                                 if (sub->level < 8)
273                                         procmime_scan_multipart_message
274                                                 (sub, fp);
275                         }
276                 }
277
278                 /* look for next boundary */
279                 buf[0] = '\0';
280                 while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
281                         if (IS_BOUNDARY(buf, boundary, boundary_len)) {
282                                 if (buf[2 + boundary_len]     == '-' &&
283                                     buf[2 + boundary_len + 1] == '-')
284                                         eom = TRUE;
285                                 break;
286                         }
287                 }
288                 if (p == NULL)
289                         eom = TRUE;     /* broken MIME message */
290                 fpos = ftell(fp);
291
292                 partinfo->size = fpos - prev_fpos - strlen(buf);
293
294                 if (eom) break;
295         }
296         /*g_message ("** at " __PRETTY_FUNCTION__ ":%d:", __LINE__);*/
297 }
298
299 void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
300 {
301         gchar *buf;
302
303         Xstrdup_a(buf, encoding, return);
304
305         g_free(mimeinfo->encoding);
306
307         mimeinfo->encoding = g_strdup(g_strstrip(buf));
308         if (!strcasecmp(buf, "7bit"))
309                 mimeinfo->encoding_type = ENC_7BIT;
310         else if (!strcasecmp(buf, "8bit"))
311                 mimeinfo->encoding_type = ENC_8BIT;
312         else if (!strcasecmp(buf, "quoted-printable"))
313                 mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
314         else if (!strcasecmp(buf, "base64"))
315                 mimeinfo->encoding_type = ENC_BASE64;
316         else if (!strcasecmp(buf, "x-uuencode"))
317                 mimeinfo->encoding_type = ENC_X_UUENCODE;
318         else
319                 mimeinfo->encoding_type = ENC_UNKNOWN;
320
321 }
322
323 void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
324 {
325         gchar *delim, *p, *cnttype;
326         gchar *buf;
327
328         if (conv_get_current_charset() == C_EUC_JP &&
329             strchr(content_type, '\033')) {
330                 gint len;
331                 len = strlen(content_type) * 2 + 1;
332                 Xalloca(buf, len, return);
333                 conv_jistoeuc(buf, len, content_type);
334         } else
335                 Xstrdup_a(buf, content_type, return);
336
337         g_free(mimeinfo->content_type);
338         g_free(mimeinfo->charset);
339         g_free(mimeinfo->name);
340         mimeinfo->content_type = NULL;
341         mimeinfo->charset      = NULL;
342         mimeinfo->name         = NULL;
343
344         if ((delim = strchr(buf, ';'))) *delim = '\0';
345         mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
346
347         mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
348
349         if (!delim) return;
350         p = delim + 1;
351
352         for (;;) {
353                 gchar *eq;
354                 gchar *attr, *value;
355
356                 if ((delim = strchr(p, ';'))) *delim = '\0';
357
358                 if (!(eq = strchr(p, '='))) break;
359
360                 *eq = '\0';
361                 attr = p;
362                 g_strstrip(attr);
363                 value = eq + 1;
364                 g_strstrip(value);
365
366                 if (*value == '"')
367                         extract_quote(value, '"');
368                 else {
369                         eliminate_parenthesis(value, '(', ')');
370                         g_strstrip(value);
371                 }
372
373                 if (*value) {
374                         if (!strcasecmp(attr, "charset"))
375                                 mimeinfo->charset = g_strdup(value);
376                         else if (!strcasecmp(attr, "name")) {
377                                 gchar *tmp;
378                                 size_t len;
379
380                                 len = strlen(value) + 1;
381                                 Xalloca(tmp, len, return);
382                                 conv_unmime_header(tmp, len, value, NULL);
383                                 mimeinfo->name = g_strdup(tmp);
384                         } else if (!strcasecmp(attr, "boundary"))
385                                 mimeinfo->boundary = g_strdup(value);
386                 }
387
388                 if (!delim) break;
389                 p = delim + 1;
390         }
391
392         if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
393                 mimeinfo->mime_type = MIME_TEXT;
394 }
395
396 void procmime_scan_content_disposition(MimeInfo *mimeinfo,
397                                        const gchar *content_disposition)
398 {
399         gchar *delim, *p, *dispos;
400         gchar *buf;
401
402         if (conv_get_current_charset() == C_EUC_JP &&
403             strchr(content_disposition, '\033')) {
404                 gint len;
405                 len = strlen(content_disposition) * 2 + 1;
406                 Xalloca(buf, len, return);
407                 conv_jistoeuc(buf, len, content_disposition);
408         } else
409                 Xstrdup_a(buf, content_disposition, return);
410
411         if ((delim = strchr(buf, ';'))) *delim = '\0';
412         mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
413
414         if (!delim) return;
415         p = delim + 1;
416
417         for (;;) {
418                 gchar *eq;
419                 gchar *attr, *value;
420
421                 if ((delim = strchr(p, ';'))) *delim = '\0';
422
423                 if (!(eq = strchr(p, '='))) break;
424
425                 *eq = '\0';
426                 attr = p;
427                 g_strstrip(attr);
428                 value = eq + 1;
429                 g_strstrip(value);
430
431                 if (*value == '"')
432                         extract_quote(value, '"');
433                 else {
434                         eliminate_parenthesis(value, '(', ')');
435                         g_strstrip(value);
436                 }
437
438                 if (*value) {
439                         if (!strcasecmp(attr, "filename")) {
440                                 gchar *tmp;
441                                 size_t len;
442
443                                 len = strlen(value) + 1;
444                                 Xalloca(tmp, len, return);
445                                 conv_unmime_header(tmp, len, value, NULL);
446                                 g_free(mimeinfo->filename);
447                                 mimeinfo->filename = g_strdup(tmp);
448                                 break;
449                         }
450                 }
451
452                 if (!delim) break;
453                 p = delim + 1;
454         }
455 }
456
457 enum
458 {
459         H_CONTENT_TRANSFER_ENCODING = 0,
460         H_CONTENT_TYPE              = 1,
461         H_CONTENT_DISPOSITION       = 2
462 };
463
464 MimeInfo *procmime_scan_mime_header(FILE *fp)
465 {
466         static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
467                                                           NULL, FALSE},
468                                        {"Content-Type:", NULL, TRUE},
469                                        {"Content-Disposition:",
470                                                           NULL, TRUE},
471                                        {NULL,             NULL, FALSE}};
472         gchar buf[BUFFSIZE];
473         gint hnum;
474         HeaderEntry *hp;
475         MimeInfo *mimeinfo;
476
477         g_return_val_if_fail(fp != NULL, NULL);
478
479         mimeinfo = procmime_mimeinfo_new();
480         mimeinfo->mime_type = MIME_TEXT;
481         mimeinfo->encoding_type = ENC_7BIT;
482         mimeinfo->fpos = ftell(fp);
483
484         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
485                != -1) {
486                 hp = hentry + hnum;
487
488                 if (H_CONTENT_TRANSFER_ENCODING == hnum) {
489                         procmime_scan_encoding
490                                 (mimeinfo, buf + strlen(hp->name));
491                 } else if (H_CONTENT_TYPE == hnum) {
492                         procmime_scan_content_type
493                                 (mimeinfo, buf + strlen(hp->name));
494                 } else if (H_CONTENT_DISPOSITION == hnum) {
495                         procmime_scan_content_disposition
496                                 (mimeinfo, buf + strlen(hp->name));
497                 }
498         }
499
500         if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
501             mimeinfo->name) {
502                 const gchar *type;
503                 type = procmime_get_mime_type(mimeinfo->name);
504                 if (type)
505                         mimeinfo->mime_type = procmime_scan_mime_type(type);
506         }
507
508         if (!mimeinfo->content_type)
509                 mimeinfo->content_type = g_strdup("text/plain");
510
511         return mimeinfo;
512 }
513
514 FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
515 {
516         gchar buf[BUFFSIZE];
517         gchar *boundary = NULL;
518         gint boundary_len = 0;
519         gboolean tmp_file = FALSE;
520
521         g_return_val_if_fail(infp != NULL, NULL);
522         g_return_val_if_fail(mimeinfo != NULL, NULL);
523
524         if (!outfp) {
525                 outfp = my_tmpfile();
526                 if (!outfp) {
527                         perror("tmpfile");
528                         return NULL;
529                 }
530                 tmp_file = TRUE;
531         }
532
533         if (mimeinfo->parent && mimeinfo->parent->boundary) {
534                 boundary = mimeinfo->parent->boundary;
535                 boundary_len = strlen(boundary);
536         }
537
538         if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
539                 gboolean softline = FALSE;
540
541                 while (fgets(buf, sizeof(buf), infp) != NULL &&
542                        (!boundary ||
543                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
544                         guchar *p = buf;
545
546                         softline = DoOneQPLine(&p, FALSE, softline);
547                         fwrite(buf, p - (guchar *)buf, 1, outfp);
548                 }
549         } else if (mimeinfo->encoding_type == ENC_BASE64) {
550                 gchar outbuf[BUFFSIZE];
551                 gint len;
552                 Base64Decoder *decoder;
553
554                 decoder = base64_decoder_new();
555                 while (fgets(buf, sizeof(buf), infp) != NULL &&
556                        (!boundary ||
557                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
558                         len = base64_decoder_decode(decoder, buf, outbuf);
559                         if (len < 0) {
560                                 g_warning("Bad BASE64 content\n");
561                                 break;
562                         }
563                         fwrite(outbuf, sizeof(gchar), len, outfp);
564                 }
565                 base64_decoder_free(decoder);
566         } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
567                 gchar outbuf[BUFFSIZE];
568                 gint len;
569                 gboolean flag = FALSE;
570
571                 while (fgets(buf, sizeof(buf), infp) != NULL &&
572                        (!boundary ||
573                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
574                         if(!flag && strncmp(buf,"begin ", 6)) continue;
575
576                         if (flag) {
577                                 len = fromuutobits(outbuf, buf);
578                                 if (len <= 0) {
579                                         if (len < 0) 
580                                                 g_warning("Bad UUENCODE content(%d)\n", len);
581                                         break;
582                                 }
583                                 fwrite(outbuf, sizeof(gchar), len, outfp);
584                         } else
585                                 flag = TRUE;
586                 }
587         } else {
588                 while (fgets(buf, sizeof(buf), infp) != NULL &&
589                        (!boundary ||
590                         !IS_BOUNDARY(buf, boundary, boundary_len))) {
591                         fputs(buf, outfp);
592                 }
593         }
594
595         if (tmp_file) rewind(outfp);
596         return outfp;
597 }
598
599 gint procmime_get_part(const gchar *outfile, const gchar *infile,
600                        MimeInfo *mimeinfo)
601 {
602         FILE *infp, *outfp;
603         gchar buf[BUFFSIZE];
604
605         g_return_val_if_fail(outfile != NULL, -1);
606         g_return_val_if_fail(infile != NULL, -1);
607         g_return_val_if_fail(mimeinfo != NULL, -1);
608
609         if ((infp = fopen(infile, "r")) == NULL) {
610                 FILE_OP_ERROR(infile, "fopen");
611                 return -1;
612         }
613         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
614                 FILE_OP_ERROR(infile, "fseek");
615                 fclose(infp);
616                 return -1;
617         }
618         if ((outfp = fopen(outfile, "w")) == NULL) {
619                 FILE_OP_ERROR(outfile, "fopen");
620                 fclose(infp);
621                 return -1;
622         }
623
624         while (fgets(buf, sizeof(buf), infp) != NULL)
625                 if (buf[0] == '\r' || buf[0] == '\n') break;
626
627         procmime_decode_content(outfp, infp, mimeinfo);
628
629         fclose(infp);
630         if (fclose(outfp) == EOF) {
631                 FILE_OP_ERROR(outfile, "fclose");
632                 unlink(outfile);
633                 return -1;
634         }
635
636         return 0;
637 }
638
639 FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
640 {
641         FILE *tmpfp, *outfp;
642         gchar *src_codeset;
643         gboolean conv_fail = FALSE;
644         gchar buf[BUFFSIZE];
645
646         g_return_val_if_fail(mimeinfo != NULL, NULL);
647         g_return_val_if_fail(infp != NULL, NULL);
648         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
649                              mimeinfo->mime_type == MIME_TEXT_HTML, NULL);
650
651         if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
652                 perror("fseek");
653                 return NULL;
654         }
655
656         while (fgets(buf, sizeof(buf), infp) != NULL)
657                 if (buf[0] == '\r' || buf[0] == '\n') break;
658
659         tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
660         if (!tmpfp)
661                 return NULL;
662
663         if ((outfp = my_tmpfile()) == NULL) {
664                 perror("tmpfile");
665                 fclose(tmpfp);
666                 return NULL;
667         }
668
669         src_codeset = prefs_common.force_charset
670                 ? prefs_common.force_charset : mimeinfo->charset;
671
672         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
673                 gchar *str;
674
675                 str = conv_codeset_strdup(buf, src_codeset, NULL);
676                 if (str) {
677                         fputs(str, outfp);
678                         g_free(str);
679                 } else {
680                         conv_fail = TRUE;
681                         fputs(buf, outfp);
682                 }
683         }
684         if (conv_fail) g_warning(_("Code conversion failed.\n"));
685
686         fclose(tmpfp);
687         rewind(outfp);
688
689         return outfp;
690 }
691
692 /* search the first text part of (multipart) MIME message,
693    decode, convert it and output to outfp. */
694 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
695 {
696         FILE *infp, *tmpfp, *outfp;
697         MimeInfo *mimeinfo, *partinfo = NULL;
698         gchar *src_codeset;
699         gboolean conv_fail = FALSE;
700         gchar buf[BUFFSIZE];
701
702         g_return_val_if_fail(msginfo != NULL, NULL);
703
704         mimeinfo = procmime_scan_message(msginfo);
705         if (!mimeinfo) return NULL;
706
707         if ((infp = procmsg_open_message(msginfo)) == NULL) {
708                 procmime_mimeinfo_free_all(mimeinfo);
709                 return NULL;
710         }
711
712         partinfo = mimeinfo;
713         while (partinfo && partinfo->mime_type == MIME_MULTIPART)
714                 partinfo = partinfo->children;
715
716         if (partinfo && partinfo->mime_type == MIME_TEXT) {
717                 if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
718                         perror("fseek");
719                         partinfo = NULL;
720                 }
721         }
722
723         if (!partinfo) {
724                 procmime_mimeinfo_free_all(mimeinfo);
725                 return NULL;
726         }
727
728         while (fgets(buf, sizeof(buf), infp) != NULL)
729                 if (buf[0] == '\r' || buf[0] == '\n') break;
730
731         tmpfp = procmime_decode_content(NULL, infp, partinfo);
732         if (!tmpfp) {
733                 procmime_mimeinfo_free_all(mimeinfo);
734                 return NULL;
735         }
736
737         if ((outfp = my_tmpfile()) == NULL) {
738                 perror("tmpfile");
739                 fclose(tmpfp);
740                 procmime_mimeinfo_free_all(mimeinfo);
741                 return NULL;
742         }
743
744         src_codeset = prefs_common.force_charset
745                 ? prefs_common.force_charset : partinfo->charset;
746
747         while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
748                 gchar *str;
749
750                 str = conv_codeset_strdup(buf, src_codeset, NULL);
751                 if (str) {
752                         fputs(str, outfp);
753                         g_free(str);
754                 } else {
755                         conv_fail = TRUE;
756                         fputs(buf, outfp);
757                 }
758         }
759         if (conv_fail) g_warning(_("Code conversion failed.\n"));
760
761         fclose(tmpfp);
762         procmime_mimeinfo_free_all(mimeinfo);
763         rewind(outfp);
764
765         return outfp;
766 }
767
768 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
769                                    const gchar *str, gboolean case_sens)
770 {
771
772         FILE *infp, *outfp;
773         gchar buf[BUFFSIZE];
774         gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
775
776         g_return_val_if_fail(mimeinfo != NULL, FALSE);
777         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
778                              mimeinfo->mime_type == MIME_TEXT_HTML, FALSE);
779         g_return_val_if_fail(str != NULL, FALSE);
780
781         if ((infp = fopen(filename, "r")) == NULL) {
782                 FILE_OP_ERROR(filename, "fopen");
783                 return FALSE;
784         }
785
786         outfp = procmime_get_text_content(mimeinfo, infp);
787         fclose(infp);
788
789         if (!outfp)
790                 return FALSE;
791
792         if (case_sens)
793                 StrFindFunc = strstr;
794         else
795                 StrFindFunc = strcasestr;
796
797         while (fgets(buf, sizeof(buf), outfp) != NULL) {
798                 if (StrFindFunc(buf, str) != NULL) {
799                         fclose(outfp);
800                         return TRUE;
801                 }
802         }
803
804         fclose(outfp);
805
806         return FALSE;
807 }
808
809 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
810                               gboolean case_sens)
811 {
812         MimeInfo *mimeinfo;
813         MimeInfo *partinfo;
814         gchar *filename;
815         gboolean found = FALSE;
816
817         g_return_val_if_fail(msginfo != NULL, NULL);
818         g_return_val_if_fail(str != NULL, NULL);
819
820         filename = procmsg_get_message_file(msginfo);
821         if (!filename) return FALSE;
822         mimeinfo = procmime_scan_message(msginfo);
823
824         for (partinfo = mimeinfo; partinfo != NULL;
825              partinfo = procmime_mimeinfo_next(partinfo)) {
826                 if (partinfo->mime_type == MIME_TEXT ||
827                     partinfo->mime_type == MIME_TEXT_HTML) {
828                         if (procmime_find_string_part
829                                 (partinfo, filename, str, case_sens) == TRUE) {
830                                 found = TRUE;
831                                 break;
832                         }
833                 }
834         }
835
836         procmime_mimeinfo_free_all(mimeinfo);
837         g_free(filename);
838
839         return found;
840 }
841
842 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
843 {
844         static guint32 id = 0;
845         gchar *base;
846         gchar *filename;
847         gchar f_prefix[10];
848
849         g_return_val_if_fail(mimeinfo != NULL, NULL);
850
851         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
852
853         if (MIME_TEXT_HTML == mimeinfo->mime_type)
854                 base = "mimetmp.html";
855         else {
856                 base = mimeinfo->filename ? mimeinfo->filename
857                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
858                 base = g_basename(base);
859                 if (*base == '\0') base = "mimetmp";
860         }
861
862         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
863                                f_prefix, base, NULL);
864
865         return filename;
866 }
867
868 ContentType procmime_scan_mime_type(const gchar *mime_type)
869 {
870         ContentType type;
871
872         if (!strncasecmp(mime_type, "text/html", 9))
873                 type = MIME_TEXT_HTML;
874         else if (!strncasecmp(mime_type, "text/", 5))
875                 type = MIME_TEXT;
876         else if (!strncasecmp(mime_type, "message/rfc822", 14))
877                 type = MIME_MESSAGE_RFC822;
878         else if (!strncasecmp(mime_type, "message/", 8))
879                 type = MIME_TEXT;
880         else if (!strncasecmp(mime_type, "application/octet-stream", 24))
881                 type = MIME_APPLICATION_OCTET_STREAM;
882         else if (!strncasecmp(mime_type, "application/", 12))
883                 type = MIME_APPLICATION;
884         else if (!strncasecmp(mime_type, "multipart/", 10))
885                 type = MIME_MULTIPART;
886         else if (!strncasecmp(mime_type, "image/", 6))
887                 type = MIME_IMAGE;
888         else if (!strncasecmp(mime_type, "audio/", 6))
889                 type = MIME_AUDIO;
890         else if (!strcasecmp(mime_type, "text"))
891                 type = MIME_TEXT;
892         else
893                 type = MIME_UNKNOWN;
894
895         return type;
896 }
897
898 static GList *mime_type_list = NULL;
899
900 gchar *procmime_get_mime_type(const gchar *filename)
901 {
902         static GHashTable *mime_type_table = NULL;
903         MimeType *mime_type;
904         const gchar *ext, *p;
905
906         if (!mime_type_table) {
907                 mime_type_table = procmime_get_mime_type_table();
908                 if (!mime_type_table) return NULL;
909         }
910
911         filename = g_basename(filename);
912         p = strrchr(filename, '.');
913         if (p)
914                 ext = p + 1;
915         else
916                 return NULL;
917
918         mime_type = g_hash_table_lookup(mime_type_table, ext);
919         if (mime_type) {
920                 gchar *str;
921
922                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
923                                   NULL);
924                 return str;
925         }
926
927         return NULL;
928 }
929
930 static guint procmime_str_hash(gconstpointer gptr)
931 {
932         guint hash_result = 0;
933         const char *str;
934
935         for (str = gptr; str && *str; str++) {
936                 if (isupper(*str)) hash_result += (*str + ' ');
937                 else hash_result += *str;
938         }
939
940         return hash_result;
941 }
942
943 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
944 {
945         const char *str1 = gptr1;
946         const char *str2 = gptr2;
947
948         return !strcasecmp(str1, str2);
949 }
950
951 static GHashTable *procmime_get_mime_type_table(void)
952 {
953         GHashTable *table = NULL;
954         GList *cur;
955         MimeType *mime_type;
956         gchar **exts;
957
958         if (!mime_type_list) {
959                 mime_type_list = procmime_get_mime_type_list();
960                 if (!mime_type_list) return NULL;
961         }
962
963         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
964
965         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
966                 gint i;
967
968                 mime_type = (MimeType *)cur->data;
969
970                 if (!mime_type->extension) continue;
971
972                 exts = g_strsplit(mime_type->extension, " ", 16);
973                 for (i = 0; exts[i] != NULL; i++)
974                         g_hash_table_insert(table, g_strdup(exts[i]),
975                                             mime_type);
976                 g_strfreev(exts);
977         }
978
979         return table;
980 }
981
982 GList *procmime_get_mime_type_list(void)
983 {
984         GList *list = NULL;
985         FILE *fp;
986         gchar buf[BUFFSIZE];
987         gchar *p, *delim;
988         MimeType *mime_type;
989
990         if (mime_type_list) 
991                 return mime_type_list;
992
993         if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
994                 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
995                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
996                         return NULL;
997                 }
998         }
999
1000         while (fgets(buf, sizeof(buf), fp) != NULL) {
1001                 p = strchr(buf, '#');
1002                 if (p) *p = '\0';
1003                 g_strstrip(buf);
1004
1005                 p = buf;
1006                 while (*p && !isspace(*p)) p++;
1007                 if (*p) {
1008                         *p = '\0';
1009                         p++;
1010                 }
1011                 delim = strchr(buf, '/');
1012                 if (delim == NULL) continue;
1013                 *delim = '\0';
1014
1015                 mime_type = g_new(MimeType, 1);
1016                 mime_type->type = g_strdup(buf);
1017                 mime_type->sub_type = g_strdup(delim + 1);
1018
1019                 while (*p && isspace(*p)) p++;
1020                 if (*p)
1021                         mime_type->extension = g_strdup(p);
1022                 else
1023                         mime_type->extension = NULL;
1024
1025                 list = g_list_append(list, mime_type);
1026         }
1027
1028         fclose(fp);
1029
1030         if (!list)
1031                 g_warning("Can't read mime.types\n");
1032
1033         return list;
1034 }
1035
1036 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1037 {
1038         if (!charset)
1039                 return ENC_8BIT;
1040         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1041                  !strcasecmp(charset, "US-ASCII"))
1042                 return ENC_7BIT;
1043         else
1044                 return ENC_8BIT;
1045                 /* return ENC_BASE64; */
1046                 /* return ENC_QUOTED_PRINTABLE; */
1047 }
1048
1049 const gchar *procmime_get_encoding_str(EncodingType encoding)
1050 {
1051         static const gchar *encoding_str[] = {
1052                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1053                 NULL
1054         };
1055
1056         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1057                 return encoding_str[encoding];
1058         else
1059                 return NULL;
1060 }