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