sync with sylpheed 0.6.2cvs8
[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)
685                 g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
686
687         fclose(tmpfp);
688         rewind(outfp);
689
690         return outfp;
691 }
692
693 /* search the first text part of (multipart) MIME message,
694    decode, convert it and output to outfp. */
695 FILE *procmime_get_first_text_content(MsgInfo *msginfo)
696 {
697         FILE *infp, *outfp = NULL;
698         MimeInfo *mimeinfo, *partinfo;
699
700         g_return_val_if_fail(msginfo != NULL, NULL);
701
702         mimeinfo = procmime_scan_message(msginfo);
703         if (!mimeinfo) return NULL;
704
705         if ((infp = procmsg_open_message(msginfo)) == NULL) {
706                 procmime_mimeinfo_free_all(mimeinfo);
707                 return NULL;
708         }
709
710         partinfo = mimeinfo;
711         while (partinfo && partinfo->mime_type != MIME_TEXT)
712                 partinfo = procmime_mimeinfo_next(partinfo);
713
714         if (partinfo)
715                 outfp = procmime_get_text_content(partinfo, infp);
716
717         fclose(infp);
718         procmime_mimeinfo_free_all(mimeinfo);
719
720         return outfp;
721 }
722
723 gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
724                                    const gchar *str, gboolean case_sens)
725 {
726
727         FILE *infp, *outfp;
728         gchar buf[BUFFSIZE];
729         gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
730
731         g_return_val_if_fail(mimeinfo != NULL, FALSE);
732         g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
733                              mimeinfo->mime_type == MIME_TEXT_HTML, FALSE);
734         g_return_val_if_fail(str != NULL, FALSE);
735
736         if ((infp = fopen(filename, "r")) == NULL) {
737                 FILE_OP_ERROR(filename, "fopen");
738                 return FALSE;
739         }
740
741         outfp = procmime_get_text_content(mimeinfo, infp);
742         fclose(infp);
743
744         if (!outfp)
745                 return FALSE;
746
747         if (case_sens)
748                 StrFindFunc = strstr;
749         else
750                 StrFindFunc = strcasestr;
751
752         while (fgets(buf, sizeof(buf), outfp) != NULL) {
753                 if (StrFindFunc(buf, str) != NULL) {
754                         fclose(outfp);
755                         return TRUE;
756                 }
757         }
758
759         fclose(outfp);
760
761         return FALSE;
762 }
763
764 gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
765                               gboolean case_sens)
766 {
767         MimeInfo *mimeinfo;
768         MimeInfo *partinfo;
769         gchar *filename;
770         gboolean found = FALSE;
771
772         g_return_val_if_fail(msginfo != NULL, FALSE);
773         g_return_val_if_fail(str != NULL, FALSE);
774
775         filename = procmsg_get_message_file(msginfo);
776         if (!filename) return FALSE;
777         mimeinfo = procmime_scan_message(msginfo);
778
779         for (partinfo = mimeinfo; partinfo != NULL;
780              partinfo = procmime_mimeinfo_next(partinfo)) {
781                 if (partinfo->mime_type == MIME_TEXT ||
782                     partinfo->mime_type == MIME_TEXT_HTML) {
783                         if (procmime_find_string_part
784                                 (partinfo, filename, str, case_sens) == TRUE) {
785                                 found = TRUE;
786                                 break;
787                         }
788                 }
789         }
790
791         procmime_mimeinfo_free_all(mimeinfo);
792         g_free(filename);
793
794         return found;
795 }
796
797 gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
798 {
799         static guint32 id = 0;
800         gchar *base;
801         gchar *filename;
802         gchar f_prefix[10];
803
804         g_return_val_if_fail(mimeinfo != NULL, NULL);
805
806         g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
807
808         if (MIME_TEXT_HTML == mimeinfo->mime_type)
809                 base = "mimetmp.html";
810         else {
811                 base = mimeinfo->filename ? mimeinfo->filename
812                         : mimeinfo->name ? mimeinfo->name : "mimetmp";
813                 base = g_basename(base);
814                 if (*base == '\0') base = "mimetmp";
815         }
816
817         filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
818                                f_prefix, base, NULL);
819
820         return filename;
821 }
822
823 ContentType procmime_scan_mime_type(const gchar *mime_type)
824 {
825         ContentType type;
826
827         if (!strncasecmp(mime_type, "text/html", 9))
828                 type = MIME_TEXT_HTML;
829         else if (!strncasecmp(mime_type, "text/", 5))
830                 type = MIME_TEXT;
831         else if (!strncasecmp(mime_type, "message/rfc822", 14))
832                 type = MIME_MESSAGE_RFC822;
833         else if (!strncasecmp(mime_type, "message/", 8))
834                 type = MIME_TEXT;
835         else if (!strncasecmp(mime_type, "application/octet-stream", 24))
836                 type = MIME_APPLICATION_OCTET_STREAM;
837         else if (!strncasecmp(mime_type, "application/", 12))
838                 type = MIME_APPLICATION;
839         else if (!strncasecmp(mime_type, "multipart/", 10))
840                 type = MIME_MULTIPART;
841         else if (!strncasecmp(mime_type, "image/", 6))
842                 type = MIME_IMAGE;
843         else if (!strncasecmp(mime_type, "audio/", 6))
844                 type = MIME_AUDIO;
845         else if (!strcasecmp(mime_type, "text"))
846                 type = MIME_TEXT;
847         else
848                 type = MIME_UNKNOWN;
849
850         return type;
851 }
852
853 static GList *mime_type_list = NULL;
854
855 gchar *procmime_get_mime_type(const gchar *filename)
856 {
857         static GHashTable *mime_type_table = NULL;
858         MimeType *mime_type;
859         const gchar *ext, *p;
860
861         if (!mime_type_table) {
862                 mime_type_table = procmime_get_mime_type_table();
863                 if (!mime_type_table) return NULL;
864         }
865
866         filename = g_basename(filename);
867         p = strrchr(filename, '.');
868         if (p)
869                 ext = p + 1;
870         else
871                 return NULL;
872
873         mime_type = g_hash_table_lookup(mime_type_table, ext);
874         if (mime_type) {
875                 gchar *str;
876
877                 str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
878                                   NULL);
879                 return str;
880         }
881
882         return NULL;
883 }
884
885 static guint procmime_str_hash(gconstpointer gptr)
886 {
887         guint hash_result = 0;
888         const char *str;
889
890         for (str = gptr; str && *str; str++) {
891                 if (isupper(*str)) hash_result += (*str + ' ');
892                 else hash_result += *str;
893         }
894
895         return hash_result;
896 }
897
898 static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
899 {
900         const char *str1 = gptr1;
901         const char *str2 = gptr2;
902
903         return !strcasecmp(str1, str2);
904 }
905
906 static GHashTable *procmime_get_mime_type_table(void)
907 {
908         GHashTable *table = NULL;
909         GList *cur;
910         MimeType *mime_type;
911         gchar **exts;
912
913         if (!mime_type_list) {
914                 mime_type_list = procmime_get_mime_type_list();
915                 if (!mime_type_list) return NULL;
916         }
917
918         table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
919
920         for (cur = mime_type_list; cur != NULL; cur = cur->next) {
921                 gint i;
922
923                 mime_type = (MimeType *)cur->data;
924
925                 if (!mime_type->extension) continue;
926
927                 exts = g_strsplit(mime_type->extension, " ", 16);
928                 for (i = 0; exts[i] != NULL; i++)
929                         g_hash_table_insert(table, g_strdup(exts[i]),
930                                             mime_type);
931                 g_strfreev(exts);
932         }
933
934         return table;
935 }
936
937 GList *procmime_get_mime_type_list(void)
938 {
939         GList *list = NULL;
940         FILE *fp;
941         gchar buf[BUFFSIZE];
942         gchar *p, *delim;
943         MimeType *mime_type;
944
945         if (mime_type_list) 
946                 return mime_type_list;
947
948         if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
949                 if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
950                         FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
951                         return NULL;
952                 }
953         }
954
955         while (fgets(buf, sizeof(buf), fp) != NULL) {
956                 p = strchr(buf, '#');
957                 if (p) *p = '\0';
958                 g_strstrip(buf);
959
960                 p = buf;
961                 while (*p && !isspace(*p)) p++;
962                 if (*p) {
963                         *p = '\0';
964                         p++;
965                 }
966                 delim = strchr(buf, '/');
967                 if (delim == NULL) continue;
968                 *delim = '\0';
969
970                 mime_type = g_new(MimeType, 1);
971                 mime_type->type = g_strdup(buf);
972                 mime_type->sub_type = g_strdup(delim + 1);
973
974                 while (*p && isspace(*p)) p++;
975                 if (*p)
976                         mime_type->extension = g_strdup(p);
977                 else
978                         mime_type->extension = NULL;
979
980                 list = g_list_append(list, mime_type);
981         }
982
983         fclose(fp);
984
985         if (!list)
986                 g_warning("Can't read mime.types\n");
987
988         return list;
989 }
990
991 EncodingType procmime_get_encoding_for_charset(const gchar *charset)
992 {
993         if (!charset)
994                 return ENC_8BIT;
995         else if (!strncasecmp(charset, "ISO-2022-", 9) ||
996                  !strcasecmp(charset, "US-ASCII"))
997                 return ENC_7BIT;
998         else
999                 return ENC_8BIT;
1000                 /* return ENC_BASE64; */
1001                 /* return ENC_QUOTED_PRINTABLE; */
1002 }
1003
1004 const gchar *procmime_get_encoding_str(EncodingType encoding)
1005 {
1006         static const gchar *encoding_str[] = {
1007                 "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1008                 NULL
1009         };
1010
1011         if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1012                 return encoding_str[encoding];
1013         else
1014                 return NULL;
1015 }