Statistics
| Revision:

root / src / procmime.c @ 129

History | View | Annotate | Download (25.5 kB)

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