Statistics
| Revision:

root / src / procmime.c @ 333

History | View | Annotate | Download (26 kB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2005 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 "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
static GList *procmime_get_mime_type_list        (const gchar *file);
49
50
51
MimeInfo *procmime_mimeinfo_new(void)
52
{
53
        MimeInfo *mimeinfo;
54
55
        mimeinfo = g_new0(MimeInfo, 1);
56
        mimeinfo->mime_type     = MIME_UNKNOWN;
57
        mimeinfo->encoding_type = ENC_UNKNOWN;
58
59
        return mimeinfo;
60
}
61
62
void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
63
{
64
        while (mimeinfo != NULL) {
65
                MimeInfo *next;
66
67
                g_free(mimeinfo->encoding);
68
                g_free(mimeinfo->content_type);
69
                g_free(mimeinfo->charset);
70
                g_free(mimeinfo->name);
71
                g_free(mimeinfo->boundary);
72
                g_free(mimeinfo->content_disposition);
73
                g_free(mimeinfo->filename);
74
#if USE_GPGME
75
                g_free(mimeinfo->plaintextfile);
76
                g_free(mimeinfo->sigstatus);
77
                g_free(mimeinfo->sigstatus_full);
78
#endif
79
80
                procmime_mimeinfo_free_all(mimeinfo->sub);
81
                procmime_mimeinfo_free_all(mimeinfo->children);
82
#if USE_GPGME
83
                procmime_mimeinfo_free_all(mimeinfo->plaintext);
84
#endif
85
86
                next = mimeinfo->next;
87
                g_free(mimeinfo);
88
                mimeinfo = next;
89
        }
90
}
91
92
MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
93
{
94
        MimeInfo *child = parent->children;
95
96
        if (!child)
97
                parent->children = mimeinfo;
98
        else {
99
                while (child->next != NULL)
100
                        child = child->next;
101
102
                child->next = mimeinfo;
103
        }
104
105
        mimeinfo->parent = parent;
106
        mimeinfo->level = parent->level + 1;
107
108
        return mimeinfo;
109
}
110
111
void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
112
{
113
        MimeInfo *parent = old->parent;
114
        MimeInfo *child;
115
116
        g_return_if_fail(parent != NULL);
117
        g_return_if_fail(new->next == NULL);
118
119
        for (child = parent->children; child && child != old;
120
             child = child->next)
121
                ;
122
        if (!child) {
123
                g_warning("oops: parent can't find it's own child");
124
                return;
125
        }
126
        procmime_mimeinfo_free_all(old);
127
128
        if (child == parent->children) {
129
                new->next = parent->children->next;
130
                parent->children = new;
131
        } else {
132
                new->next = child->next;
133
                child = new;
134
        }
135
}
136
137
MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
138
{
139
        if (!mimeinfo) return NULL;
140
141
        if (mimeinfo->children)
142
                return mimeinfo->children;
143
        if (mimeinfo->sub)
144
                return mimeinfo->sub;
145
        if (mimeinfo->next)
146
                return mimeinfo->next;
147
148
        if (mimeinfo->main) {
149
                mimeinfo = mimeinfo->main;
150
                if (mimeinfo->next)
151
                        return mimeinfo->next;
152
        }
153
154
        for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
155
             mimeinfo = mimeinfo->parent) {
156
                if (mimeinfo->next)
157
                        return mimeinfo->next;
158
                if (mimeinfo->main) {
159
                        mimeinfo = mimeinfo->main;
160
                        if (mimeinfo->next)
161
                                return mimeinfo->next;
162
                }
163
        }
164
165
        return NULL;
166
}
167
168
#if 0
169
void procmime_dump_mimeinfo(MimeInfo *mimeinfo)
170
{
171
        gint i;
172
173
        g_print("\n");
174
175
        for (; mimeinfo != NULL; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
176
                for (i = 0; i < mimeinfo->level; i++)
177
                        g_print("  ");
178
                g_print("%s%s\n", mimeinfo->main ? "sub: " : "",
179
                        mimeinfo->content_type);
180
        }
181
}
182
#endif
183
184
MimeInfo *procmime_scan_message(MsgInfo *msginfo)
185
{
186
        FILE *fp;
187
        MimeInfo *mimeinfo;
188
189
        g_return_val_if_fail(msginfo != NULL, NULL);
190
191
#if USE_GPGME
192
        if ((fp = procmsg_open_message_decrypted(msginfo, &mimeinfo)) == NULL)
193
                return NULL;
194
#else
195
        if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
196
        mimeinfo = procmime_scan_mime_header(fp);
197
#endif
198
199
        if (mimeinfo) {
200
                mimeinfo->size = get_left_file_size(fp);
201
                if (mimeinfo->mime_type == MIME_MULTIPART ||
202
                    mimeinfo->mime_type == MIME_MESSAGE_RFC822)
203
                        procmime_scan_multipart_message(mimeinfo, fp);
204
        }
205
206
        fclose(fp);
207
208
        return mimeinfo;
209
}
210
211
void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
212
{
213
        gchar *p;
214
        gchar *boundary;
215
        gint boundary_len = 0;
216
        gchar buf[BUFFSIZE];
217
        glong fpos, prev_fpos;
218
219
        g_return_if_fail(mimeinfo != NULL);
220
        g_return_if_fail(mimeinfo->mime_type == MIME_MULTIPART ||
221
                         mimeinfo->mime_type == MIME_MESSAGE_RFC822);
222
223
        if (mimeinfo->mime_type == MIME_MULTIPART) {
224
                g_return_if_fail(mimeinfo->boundary != NULL);
225
                g_return_if_fail(mimeinfo->sub == NULL);
226
        }
227
        g_return_if_fail(fp != NULL);
228
229
        boundary = mimeinfo->boundary;
230
231
        if (boundary) {
232
                boundary_len = strlen(boundary);
233
234
                /* look for first boundary */
235
                while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
236
                        if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
237
                if (!p) return;
238
        } else if (mimeinfo->parent && mimeinfo->parent->boundary) {
239
                boundary = mimeinfo->parent->boundary;
240
                boundary_len = strlen(boundary);
241
        }
242
243
        if ((fpos = ftell(fp)) < 0) {
244
                perror("ftell");
245
                return;
246
        }
247
248
        for (;;) {
249
                MimeInfo *partinfo;
250
                gboolean eom = FALSE;
251
                gint len;
252
253
                prev_fpos = fpos;
254
                debug_print("prev_fpos: %ld\n", fpos);
255
256
                if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
257
                        MimeInfo *sub;
258
259
                        mimeinfo->sub = sub = procmime_scan_mime_header(fp);
260
                        if (!sub) break;
261
262
                        sub->level = mimeinfo->level + 1;
263
                        sub->parent = mimeinfo->parent;
264
                        sub->main = mimeinfo;
265
266
                        partinfo = sub;
267
                } else {
268
                        partinfo = procmime_scan_mime_header(fp);
269
                        if (!partinfo) break;
270
                        procmime_mimeinfo_insert(mimeinfo, partinfo);
271
                        debug_print("content-type: %s\n",
272
                                    partinfo->content_type);
273
                }
274
275
                if (partinfo->mime_type == MIME_MULTIPART ||
276
                    partinfo->mime_type == MIME_MESSAGE_RFC822) {
277
                        if (partinfo->level < 8)
278
                                procmime_scan_multipart_message(partinfo, fp);
279
                }
280
281
                /* look for next boundary */
282
                buf[0] = '\0';
283
                while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
284
                        if (IS_BOUNDARY(buf, boundary, boundary_len)) {
285
                                if (buf[2 + boundary_len]     == '-' &&
286
                                    buf[2 + boundary_len + 1] == '-')
287
                                        eom = TRUE;
288
                                break;
289
                        }
290
                }
291
                if (p == NULL) {
292
                        /* broken MIME, or single part MIME message */
293
                        buf[0] = '\0';
294
                        eom = TRUE;
295
                }
296
                debug_print("boundary: %s\n", buf);
297
298
                fpos = ftell(fp);
299
                debug_print("fpos: %ld\n", fpos);
300
301
                len = strlen(buf);
302
                partinfo->size = fpos - prev_fpos - len;
303
                debug_print("partinfo->size: %d\n", partinfo->size);
304
                if (partinfo->sub && !partinfo->sub->sub &&
305
                    !partinfo->sub->children) {
306
                        partinfo->sub->size =
307
                                fpos - partinfo->sub->fpos - strlen(buf);
308
                        debug_print("partinfo->sub->size: %d\n",
309
                                    partinfo->sub->size);
310
                }
311
312
                if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
313
                        if (len > 0 && fseek(fp, fpos - len, SEEK_SET) < 0)
314
                                perror("fseek");
315
                        break;
316
                }
317
318
                if (eom) break;
319
        }
320
}
321
322
void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
323
{
324
        gchar *buf;
325
326
        Xstrdup_a(buf, encoding, return);
327
328
        g_free(mimeinfo->encoding);
329
330
        mimeinfo->encoding = g_strdup(g_strstrip(buf));
331
        if (!g_ascii_strcasecmp(buf, "7bit"))
332
                mimeinfo->encoding_type = ENC_7BIT;
333
        else if (!g_ascii_strcasecmp(buf, "8bit"))
334
                mimeinfo->encoding_type = ENC_8BIT;
335
        else if (!g_ascii_strcasecmp(buf, "quoted-printable"))
336
                mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
337
        else if (!g_ascii_strcasecmp(buf, "base64"))
338
                mimeinfo->encoding_type = ENC_BASE64;
339
        else if (!g_ascii_strcasecmp(buf, "x-uuencode"))
340
                mimeinfo->encoding_type = ENC_X_UUENCODE;
341
        else
342
                mimeinfo->encoding_type = ENC_UNKNOWN;
343
344
}
345
346
void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
347
{
348
        g_free(mimeinfo->content_type);
349
        g_free(mimeinfo->charset);
350
        g_free(mimeinfo->name);
351
        g_free(mimeinfo->boundary);
352
        mimeinfo->content_type = NULL;
353
        mimeinfo->charset      = NULL;
354
        mimeinfo->name         = NULL;
355
        mimeinfo->boundary     = NULL;
356
357
        procmime_scan_content_type_str(content_type, &mimeinfo->content_type,
358
                                       &mimeinfo->charset, &mimeinfo->name,
359
                                       &mimeinfo->boundary);
360
361
        mimeinfo->mime_type = procmime_scan_mime_type(mimeinfo->content_type);
362
        if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
363
                mimeinfo->mime_type = MIME_TEXT;
364
}
365
366
void procmime_scan_content_type_str(const gchar *content_type,
367
                                    gchar **mime_type, gchar **charset,
368
                                    gchar **name, gchar **boundary)
369
{
370
        gchar *delim, *p;
371
        gchar *buf;
372
373
        Xstrdup_a(buf, content_type, return);
374
375
        if ((delim = strchr(buf, ';'))) *delim = '\0';
376
        if (mime_type)
377
                *mime_type = g_strdup(g_strstrip(buf));
378
379
        if (!delim) return;
380
        p = delim + 1;
381
382
        for (;;) {
383
                gchar *eq;
384
                gchar *attr, *value;
385
386
                if ((delim = strchr(p, ';'))) *delim = '\0';
387
388
                if (!(eq = strchr(p, '='))) break;
389
390
                *eq = '\0';
391
                attr = p;
392
                g_strstrip(attr);
393
                value = eq + 1;
394
                g_strstrip(value);
395
396
                if (*value == '"')
397
                        extract_quote(value, '"');
398
                else {
399
                        eliminate_parenthesis(value, '(', ')');
400
                        g_strstrip(value);
401
                }
402
403
                if (*value) {
404
                        if (charset && !g_ascii_strcasecmp(attr, "charset"))
405
                                *charset = g_strdup(value);
406
                        else if (name && !g_ascii_strcasecmp(attr, "name"))
407
                                *name = conv_unmime_header(value, NULL);
408
                        else if (boundary &&
409
                                 !g_ascii_strcasecmp(attr, "boundary"))
410
                                *boundary = g_strdup(value);
411
                }
412
413
                if (!delim) break;
414
                p = delim + 1;
415
        }
416
}
417
418
void procmime_scan_content_disposition(MimeInfo *mimeinfo,
419
                                       const gchar *content_disposition)
420
{
421
        gchar *delim, *p, *dispos;
422
        gchar *buf;
423
424
        Xstrdup_a(buf, content_disposition, return);
425
426
        if ((delim = strchr(buf, ';'))) *delim = '\0';
427
        mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
428
429
        if (!delim) return;
430
        p = delim + 1;
431
432
        for (;;) {
433
                gchar *eq;
434
                gchar *attr, *value;
435
436
                if ((delim = strchr(p, ';'))) *delim = '\0';
437
438
                if (!(eq = strchr(p, '='))) break;
439
440
                *eq = '\0';
441
                attr = p;
442
                g_strstrip(attr);
443
                value = eq + 1;
444
                g_strstrip(value);
445
446
                if (*value == '"')
447
                        extract_quote(value, '"');
448
                else {
449
                        eliminate_parenthesis(value, '(', ')');
450
                        g_strstrip(value);
451
                }
452
453
                if (*value) {
454
                        if (!g_ascii_strcasecmp(attr, "filename")) {
455
                                g_free(mimeinfo->filename);
456
                                mimeinfo->filename =
457
                                        conv_unmime_header(value, NULL);
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
689
        g_return_val_if_fail(mimeinfo != NULL, NULL);
690
        g_return_val_if_fail(infp != NULL, NULL);
691
        g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
692
                             mimeinfo->mime_type == MIME_TEXT_HTML, NULL);
693
694
        if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
695
                perror("fseek");
696
                return NULL;
697
        }
698
699
        while (fgets(buf, sizeof(buf), infp) != NULL)
700
                if (buf[0] == '\r' || buf[0] == '\n') break;
701
702
        tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
703
        if (!tmpfp)
704
                return NULL;
705
706
        if ((outfp = my_tmpfile()) == NULL) {
707
                perror("tmpfile");
708
                fclose(tmpfp);
709
                return NULL;
710
        }
711
712
        src_encoding = prefs_common.force_charset
713
                ? prefs_common.force_charset : mimeinfo->charset;
714
715
        if (mimeinfo->mime_type == MIME_TEXT) {
716
                while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
717
                        gchar *str;
718
719
                        str = conv_codeset_strdup(buf, src_encoding, encoding);
720
                        if (str) {
721
                                fputs(str, outfp);
722
                                g_free(str);
723
                        } else {
724
                                conv_fail = TRUE;
725
                                fputs(buf, outfp);
726
                        }
727
                }
728
        } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
729
                HTMLParser *parser;
730
                CodeConverter *conv;
731
                const gchar *str;
732
733
                conv = conv_code_converter_new(src_encoding, encoding);
734
                parser = html_parser_new(tmpfp, conv);
735
                while ((str = html_parse(parser)) != NULL) {
736
                        fputs(str, outfp);
737
                }
738
                html_parser_destroy(parser);
739
                conv_code_converter_destroy(conv);
740
        }
741
742
        if (conv_fail)
743
                g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
744
745
        fclose(tmpfp);
746
        rewind(outfp);
747
748
        return outfp;
749
}
750
751
/* search the first text part of (multipart) MIME message,
752
   decode, convert it and output to outfp. */
753
FILE *procmime_get_first_text_content(MsgInfo *msginfo, const gchar *encoding)
754
{
755
        FILE *infp, *outfp = NULL;
756
        MimeInfo *mimeinfo, *partinfo;
757
758
        g_return_val_if_fail(msginfo != NULL, NULL);
759
760
        mimeinfo = procmime_scan_message(msginfo);
761
        if (!mimeinfo) return NULL;
762
763
        if ((infp = procmsg_open_message(msginfo)) == NULL) {
764
                procmime_mimeinfo_free_all(mimeinfo);
765
                return NULL;
766
        }
767
768
        partinfo = mimeinfo;
769
        while (partinfo && partinfo->mime_type != MIME_TEXT)
770
                partinfo = procmime_mimeinfo_next(partinfo);
771
        if (!partinfo) {
772
                partinfo = mimeinfo;
773
                while (partinfo && partinfo->mime_type != MIME_TEXT_HTML)
774
                        partinfo = procmime_mimeinfo_next(partinfo);
775
        }
776
777
        if (partinfo)
778
                outfp = procmime_get_text_content(partinfo, infp, encoding);
779
780
        fclose(infp);
781
        procmime_mimeinfo_free_all(mimeinfo);
782
783
        return outfp;
784
}
785
786
gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
787
                                   const gchar *str, StrFindFunc find_func)
788
{
789
790
        FILE *infp, *outfp;
791
        gchar buf[BUFFSIZE];
792
793
        g_return_val_if_fail(mimeinfo != NULL, FALSE);
794
        g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
795
                             mimeinfo->mime_type == MIME_TEXT_HTML, FALSE);
796
        g_return_val_if_fail(str != NULL, FALSE);
797
        g_return_val_if_fail(find_func != NULL, FALSE);
798
799
        if ((infp = fopen(filename, "rb")) == NULL) {
800
                FILE_OP_ERROR(filename, "fopen");
801
                return FALSE;
802
        }
803
804
        outfp = procmime_get_text_content(mimeinfo, infp, NULL);
805
        fclose(infp);
806
807
        if (!outfp)
808
                return FALSE;
809
810
        while (fgets(buf, sizeof(buf), outfp) != NULL) {
811
                strretchomp(buf);
812
                if (find_func(buf, str)) {
813
                        fclose(outfp);
814
                        return TRUE;
815
                }
816
        }
817
818
        fclose(outfp);
819
820
        return FALSE;
821
}
822
823
gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
824
                              StrFindFunc find_func)
825
{
826
        MimeInfo *mimeinfo;
827
        MimeInfo *partinfo;
828
        gchar *filename;
829
        gboolean found = FALSE;
830
831
        g_return_val_if_fail(msginfo != NULL, FALSE);
832
        g_return_val_if_fail(str != NULL, FALSE);
833
        g_return_val_if_fail(find_func != NULL, FALSE);
834
835
        filename = procmsg_get_message_file(msginfo);
836
        if (!filename) return FALSE;
837
        mimeinfo = procmime_scan_message(msginfo);
838
839
        for (partinfo = mimeinfo; partinfo != NULL;
840
             partinfo = procmime_mimeinfo_next(partinfo)) {
841
                if (partinfo->mime_type == MIME_TEXT ||
842
                    partinfo->mime_type == MIME_TEXT_HTML) {
843
                        if (procmime_find_string_part
844
                                (partinfo, filename, str, find_func) == TRUE) {
845
                                found = TRUE;
846
                                break;
847
                        }
848
                }
849
        }
850
851
        procmime_mimeinfo_free_all(mimeinfo);
852
        g_free(filename);
853
854
        return found;
855
}
856
857
gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
858
{
859
        gchar *base;
860
        const gchar *base_;
861
862
        base_ = mimeinfo->filename ? mimeinfo->filename
863
                : mimeinfo->name ? mimeinfo->name : "mimetmp";
864
        base_ = g_basename(base_);
865
        if (*base_ == '\0') base_ = "mimetmp";
866
        base = conv_filename_from_utf8(base_);
867
        subst_for_filename(base);
868
869
        return base;
870
}
871
872
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
873
{
874
        static guint32 id = 0;
875
        gchar *base;
876
        gchar *filename;
877
        gchar f_prefix[10];
878
879
        g_return_val_if_fail(mimeinfo != NULL, NULL);
880
881
        g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
882
883
        if (MIME_TEXT_HTML == mimeinfo->mime_type)
884
                base = g_strdup("mimetmp.html");
885
        else
886
                base = procmime_get_part_file_name(mimeinfo);
887
888
        filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
889
                               f_prefix, base, NULL);
890
891
        g_free(base);
892
893
        return filename;
894
}
895
896
ContentType procmime_scan_mime_type(const gchar *mime_type)
897
{
898
        ContentType type;
899
900
        if (!g_ascii_strncasecmp(mime_type, "text/html", 9))
901
                type = MIME_TEXT_HTML;
902
        else if (!g_ascii_strncasecmp(mime_type, "text/", 5))
903
                type = MIME_TEXT;
904
        else if (!g_ascii_strncasecmp(mime_type, "message/rfc822", 14))
905
                type = MIME_MESSAGE_RFC822;
906
        else if (!g_ascii_strncasecmp(mime_type, "message/", 8))
907
                type = MIME_TEXT;
908
        else if (!g_ascii_strncasecmp(mime_type, "application/octet-stream",
909
                                      24))
910
                type = MIME_APPLICATION_OCTET_STREAM;
911
        else if (!g_ascii_strncasecmp(mime_type, "application/", 12))
912
                type = MIME_APPLICATION;
913
        else if (!g_ascii_strncasecmp(mime_type, "multipart/", 10))
914
                type = MIME_MULTIPART;
915
        else if (!g_ascii_strncasecmp(mime_type, "image/", 6))
916
                type = MIME_IMAGE;
917
        else if (!g_ascii_strncasecmp(mime_type, "audio/", 6))
918
                type = MIME_AUDIO;
919
        else if (!g_ascii_strcasecmp(mime_type, "text"))
920
                type = MIME_TEXT;
921
        else
922
                type = MIME_UNKNOWN;
923
924
        return type;
925
}
926
927
static GList *mime_type_list = NULL;
928
929
gchar *procmime_get_mime_type(const gchar *filename)
930
{
931
        static GHashTable *mime_type_table = NULL;
932
        MimeType *mime_type;
933
        const gchar *p;
934
        gchar *ext;
935
936
        if (!mime_type_table) {
937
                mime_type_table = procmime_get_mime_type_table();
938
                if (!mime_type_table) return NULL;
939
        }
940
941
        filename = g_basename(filename);
942
        p = strrchr(filename, '.');
943
        if (!p) return NULL;
944
945
        Xstrdup_a(ext, p + 1, return NULL);
946
        g_strdown(ext);
947
        mime_type = g_hash_table_lookup(mime_type_table, ext);
948
        if (mime_type) {
949
                gchar *str;
950
951
                str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
952
                                  NULL);
953
                return str;
954
        }
955
956
        return NULL;
957
}
958
959
static GHashTable *procmime_get_mime_type_table(void)
960
{
961
        GHashTable *table = NULL;
962
        GList *cur;
963
        MimeType *mime_type;
964
        gchar **exts;
965
966
        if (!mime_type_list) {
967
                GList *list;
968
                gchar *dir;
969
970
                mime_type_list =
971
                        procmime_get_mime_type_list(SYSCONFDIR "/mime.types");
972
                if (!mime_type_list) {
973
                        list = procmime_get_mime_type_list("/etc/mime.types");
974
                        mime_type_list = g_list_concat(mime_type_list, list);
975
                }
976
                dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, RC_DIR,
977
                                  G_DIR_SEPARATOR_S, "mime.types", NULL);
978
                list = procmime_get_mime_type_list(dir);
979
                g_free(dir);
980
                mime_type_list = g_list_concat(mime_type_list, list);
981
982
                if (!mime_type_list) {
983
                        g_warning("mime.types not found\n");
984
                        return NULL;
985
                }
986
        }
987
988
        table = g_hash_table_new(g_str_hash, g_str_equal);
989
990
        for (cur = mime_type_list; cur != NULL; cur = cur->next) {
991
                gint i;
992
                gchar *key;
993
994
                mime_type = (MimeType *)cur->data;
995
996
                if (!mime_type->extension) continue;
997
998
                exts = g_strsplit(mime_type->extension, " ", 16);
999
                for (i = 0; exts[i] != NULL; i++) {
1000
                        /* make the key case insensitive */
1001
                        g_strdown(exts[i]);
1002
                        /* use previously dup'd key on overwriting */
1003
                        if (g_hash_table_lookup(table, exts[i]))
1004
                                key = exts[i];
1005
                        else
1006
                                key = g_strdup(exts[i]);
1007
                        g_hash_table_insert(table, key, mime_type);
1008
                }
1009
                g_strfreev(exts);
1010
        }
1011
1012
        return table;
1013
}
1014
1015
static GList *procmime_get_mime_type_list(const gchar *file)
1016
{
1017
        GList *list = NULL;
1018
        FILE *fp;
1019
        gchar buf[BUFFSIZE];
1020
        guchar *p;
1021
        gchar *delim;
1022
        MimeType *mime_type;
1023
1024
        if ((fp = fopen(file, "rb")) == NULL) return NULL;
1025
1026
        debug_print("Reading %s ...\n", file);
1027
1028
        while (fgets(buf, sizeof(buf), fp) != NULL) {
1029
                p = strchr(buf, '#');
1030
                if (p) *p = '\0';
1031
                g_strstrip(buf);
1032
1033
                p = buf;
1034
                while (*p && !isspace(*p)) p++;
1035
                if (*p) {
1036
                        *p = '\0';
1037
                        p++;
1038
                }
1039
                delim = strchr(buf, '/');
1040
                if (delim == NULL) continue;
1041
                *delim = '\0';
1042
1043
                mime_type = g_new(MimeType, 1);
1044
                mime_type->type = g_strdup(buf);
1045
                mime_type->sub_type = g_strdup(delim + 1);
1046
1047
                while (*p && isspace(*p)) p++;
1048
                if (*p)
1049
                        mime_type->extension = g_strdup(p);
1050
                else
1051
                        mime_type->extension = NULL;
1052
1053
                list = g_list_append(list, mime_type);
1054
        }
1055
1056
        fclose(fp);
1057
1058
        if (!list)
1059
                g_warning("Can't read mime.types\n");
1060
1061
        return list;
1062
}
1063
1064
EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1065
{
1066
        if (!charset)
1067
                return ENC_8BIT;
1068
        else if (!g_ascii_strncasecmp(charset, "ISO-2022-", 9) ||
1069
                 !g_ascii_strcasecmp(charset, "US-ASCII"))
1070
                return ENC_7BIT;
1071
        else if (!g_ascii_strcasecmp(charset, "ISO-8859-5") ||
1072
                 !g_ascii_strncasecmp(charset, "KOI8-", 5) ||
1073
                 !g_ascii_strcasecmp(charset, "Windows-1251"))
1074
                return ENC_8BIT;
1075
        else if (!g_ascii_strncasecmp(charset, "ISO-8859-", 9))
1076
                return ENC_QUOTED_PRINTABLE;
1077
        else
1078
                return ENC_8BIT;
1079
}
1080
1081
EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1082
{
1083
        FILE *fp;
1084
        guchar buf[BUFFSIZE];
1085
        size_t len;
1086
        size_t octet_chars = 0;
1087
        size_t total_len = 0;
1088
        gfloat octet_percentage;
1089
1090
        if ((fp = fopen(file, "rb")) == NULL) {
1091
                FILE_OP_ERROR(file, "fopen");
1092
                return ENC_UNKNOWN;
1093
        }
1094
1095
        while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1096
                guchar *p;
1097
                gint i;
1098
1099
                for (p = buf, i = 0; i < len; ++p, ++i) {
1100
                        if (*p & 0x80)
1101
                                ++octet_chars;
1102
                }
1103
                total_len += len;
1104
        }
1105
1106
        fclose(fp);
1107
1108
        if (total_len > 0)
1109
                octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1110
        else
1111
                octet_percentage = 0.0;
1112
1113
        debug_print("procmime_get_encoding_for_text_file(): "
1114
                    "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1115
                    100.0 * octet_percentage);
1116
1117
        if (octet_percentage > 0.20) {
1118
                debug_print("using BASE64\n");
1119
                return ENC_BASE64;
1120
        } else if (octet_chars > 0) {
1121
                debug_print("using quoted-printable\n");
1122
                return ENC_QUOTED_PRINTABLE;
1123
        } else {
1124
                debug_print("using 7bit\n");
1125
                return ENC_7BIT;
1126
        }
1127
}
1128
1129
const gchar *procmime_get_encoding_str(EncodingType encoding)
1130
{
1131
        static const gchar *encoding_str[] = {
1132
                "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1133
                NULL
1134
        };
1135
1136
        if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1137
                return encoding_str[encoding];
1138
        else
1139
                return NULL;
1140
}