Statistics
| Revision:

root / src / procmime.c @ 128

History | View | Annotate | Download (25.4 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
{
683
        FILE *tmpfp, *outfp;
684
        gchar *src_codeset;
685
        gboolean conv_fail = FALSE;
686
        gchar buf[BUFFSIZE];
687
        gchar *str;
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_codeset = 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
                        str = conv_codeset_strdup(buf, src_codeset, NULL);
718
                        if (str) {
719
                                fputs(str, outfp);
720
                                g_free(str);
721
                        } else {
722
                                conv_fail = TRUE;
723
                                fputs(buf, outfp);
724
                        }
725
                }
726
        } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
727
                HTMLParser *parser;
728
                CodeConverter *conv;
729
730
                conv = conv_code_converter_new(src_codeset);
731
                parser = html_parser_new(tmpfp, conv);
732
                while ((str = html_parse(parser)) != NULL) {
733
                        fputs(str, outfp);
734
                }
735
                html_parser_destroy(parser);
736
                conv_code_converter_destroy(conv);
737
        }
738
739
        if (conv_fail)
740
                g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
741
742
        fclose(tmpfp);
743
        rewind(outfp);
744
745
        return outfp;
746
}
747
748
/* search the first text part of (multipart) MIME message,
749
   decode, convert it and output to outfp. */
750
FILE *procmime_get_first_text_content(MsgInfo *msginfo)
751
{
752
        FILE *infp, *outfp = NULL;
753
        MimeInfo *mimeinfo, *partinfo;
754
755
        g_return_val_if_fail(msginfo != NULL, NULL);
756
757
        mimeinfo = procmime_scan_message(msginfo);
758
        if (!mimeinfo) return NULL;
759
760
        if ((infp = procmsg_open_message(msginfo)) == NULL) {
761
                procmime_mimeinfo_free_all(mimeinfo);
762
                return NULL;
763
        }
764
765
        partinfo = mimeinfo;
766
        while (partinfo && partinfo->mime_type != MIME_TEXT)
767
                partinfo = procmime_mimeinfo_next(partinfo);
768
        if (!partinfo) {
769
                partinfo = mimeinfo;
770
                while (partinfo && partinfo->mime_type != MIME_TEXT_HTML)
771
                        partinfo = procmime_mimeinfo_next(partinfo);
772
        }
773
774
        if (partinfo)
775
                outfp = procmime_get_text_content(partinfo, infp);
776
777
        fclose(infp);
778
        procmime_mimeinfo_free_all(mimeinfo);
779
780
        return outfp;
781
}
782
783
gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
784
                                   const gchar *str, StrFindFunc find_func)
785
{
786
787
        FILE *infp, *outfp;
788
        gchar buf[BUFFSIZE];
789
790
        g_return_val_if_fail(mimeinfo != NULL, FALSE);
791
        g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
792
                             mimeinfo->mime_type == MIME_TEXT_HTML, FALSE);
793
        g_return_val_if_fail(str != NULL, FALSE);
794
        g_return_val_if_fail(find_func != NULL, FALSE);
795
796
        if ((infp = fopen(filename, "rb")) == NULL) {
797
                FILE_OP_ERROR(filename, "fopen");
798
                return FALSE;
799
        }
800
801
        outfp = procmime_get_text_content(mimeinfo, infp);
802
        fclose(infp);
803
804
        if (!outfp)
805
                return FALSE;
806
807
        while (fgets(buf, sizeof(buf), outfp) != NULL) {
808
                strretchomp(buf);
809
                if (find_func(buf, str)) {
810
                        fclose(outfp);
811
                        return TRUE;
812
                }
813
        }
814
815
        fclose(outfp);
816
817
        return FALSE;
818
}
819
820
gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
821
                              StrFindFunc find_func)
822
{
823
        MimeInfo *mimeinfo;
824
        MimeInfo *partinfo;
825
        gchar *filename;
826
        gboolean found = FALSE;
827
828
        g_return_val_if_fail(msginfo != NULL, FALSE);
829
        g_return_val_if_fail(str != NULL, FALSE);
830
        g_return_val_if_fail(find_func != NULL, FALSE);
831
832
        filename = procmsg_get_message_file(msginfo);
833
        if (!filename) return FALSE;
834
        mimeinfo = procmime_scan_message(msginfo);
835
836
        for (partinfo = mimeinfo; partinfo != NULL;
837
             partinfo = procmime_mimeinfo_next(partinfo)) {
838
                if (partinfo->mime_type == MIME_TEXT ||
839
                    partinfo->mime_type == MIME_TEXT_HTML) {
840
                        if (procmime_find_string_part
841
                                (partinfo, filename, str, find_func) == TRUE) {
842
                                found = TRUE;
843
                                break;
844
                        }
845
                }
846
        }
847
848
        procmime_mimeinfo_free_all(mimeinfo);
849
        g_free(filename);
850
851
        return found;
852
}
853
854
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
855
{
856
        static guint32 id = 0;
857
        gchar *base;
858
        gchar *filename;
859
        gchar f_prefix[10];
860
861
        g_return_val_if_fail(mimeinfo != NULL, NULL);
862
863
        g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
864
865
        if (MIME_TEXT_HTML == mimeinfo->mime_type)
866
                base = g_strdup("mimetmp.html");
867
        else {
868
                const gchar *base_;
869
870
                base_ = mimeinfo->filename ? mimeinfo->filename
871
                        : mimeinfo->name ? mimeinfo->name : "mimetmp";
872
                base_ = g_basename(base_);
873
                if (*base_ == '\0') base_ = "mimetmp";
874
                base = conv_filename_from_utf8(base_);
875
                subst_for_filename(base);
876
        }
877
878
        filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
879
                               f_prefix, base, NULL);
880
881
        g_free(base);
882
883
        return filename;
884
}
885
886
ContentType procmime_scan_mime_type(const gchar *mime_type)
887
{
888
        ContentType type;
889
890
        if (!strncasecmp(mime_type, "text/html", 9))
891
                type = MIME_TEXT_HTML;
892
        else if (!strncasecmp(mime_type, "text/", 5))
893
                type = MIME_TEXT;
894
        else if (!strncasecmp(mime_type, "message/rfc822", 14))
895
                type = MIME_MESSAGE_RFC822;
896
        else if (!strncasecmp(mime_type, "message/", 8))
897
                type = MIME_TEXT;
898
        else if (!strncasecmp(mime_type, "application/octet-stream", 24))
899
                type = MIME_APPLICATION_OCTET_STREAM;
900
        else if (!strncasecmp(mime_type, "application/", 12))
901
                type = MIME_APPLICATION;
902
        else if (!strncasecmp(mime_type, "multipart/", 10))
903
                type = MIME_MULTIPART;
904
        else if (!strncasecmp(mime_type, "image/", 6))
905
                type = MIME_IMAGE;
906
        else if (!strncasecmp(mime_type, "audio/", 6))
907
                type = MIME_AUDIO;
908
        else if (!strcasecmp(mime_type, "text"))
909
                type = MIME_TEXT;
910
        else
911
                type = MIME_UNKNOWN;
912
913
        return type;
914
}
915
916
static GList *mime_type_list = NULL;
917
918
gchar *procmime_get_mime_type(const gchar *filename)
919
{
920
        static GHashTable *mime_type_table = NULL;
921
        MimeType *mime_type;
922
        const gchar *p;
923
        gchar *ext;
924
925
        if (!mime_type_table) {
926
                mime_type_table = procmime_get_mime_type_table();
927
                if (!mime_type_table) return NULL;
928
        }
929
930
        filename = g_basename(filename);
931
        p = strrchr(filename, '.');
932
        if (!p) return NULL;
933
934
        Xstrdup_a(ext, p + 1, return NULL);
935
        g_strdown(ext);
936
        mime_type = g_hash_table_lookup(mime_type_table, ext);
937
        if (mime_type) {
938
                gchar *str;
939
940
                str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
941
                                  NULL);
942
                return str;
943
        }
944
945
        return NULL;
946
}
947
948
static GHashTable *procmime_get_mime_type_table(void)
949
{
950
        GHashTable *table = NULL;
951
        GList *cur;
952
        MimeType *mime_type;
953
        gchar **exts;
954
955
        if (!mime_type_list) {
956
                GList *list;
957
                gchar *dir;
958
959
                mime_type_list =
960
                        procmime_get_mime_type_list(SYSCONFDIR "/mime.types");
961
                if (!mime_type_list) {
962
                        list = procmime_get_mime_type_list("/etc/mime.types");
963
                        mime_type_list = g_list_concat(mime_type_list, list);
964
                }
965
                dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, RC_DIR,
966
                                  G_DIR_SEPARATOR_S, "mime.types", NULL);
967
                list = procmime_get_mime_type_list(dir);
968
                g_free(dir);
969
                mime_type_list = g_list_concat(mime_type_list, list);
970
971
                if (!mime_type_list) {
972
                        g_warning("mime.types not found\n");
973
                        return NULL;
974
                }
975
        }
976
977
        table = g_hash_table_new(g_str_hash, g_str_equal);
978
979
        for (cur = mime_type_list; cur != NULL; cur = cur->next) {
980
                gint i;
981
                gchar *key;
982
983
                mime_type = (MimeType *)cur->data;
984
985
                if (!mime_type->extension) continue;
986
987
                exts = g_strsplit(mime_type->extension, " ", 16);
988
                for (i = 0; exts[i] != NULL; i++) {
989
                        /* make the key case insensitive */
990
                        g_strdown(exts[i]);
991
                        /* use previously dup'd key on overwriting */
992
                        if (g_hash_table_lookup(table, exts[i]))
993
                                key = exts[i];
994
                        else
995
                                key = g_strdup(exts[i]);
996
                        g_hash_table_insert(table, key, mime_type);
997
                }
998
                g_strfreev(exts);
999
        }
1000
1001
        return table;
1002
}
1003
1004
static GList *procmime_get_mime_type_list(const gchar *file)
1005
{
1006
        GList *list = NULL;
1007
        FILE *fp;
1008
        gchar buf[BUFFSIZE];
1009
        guchar *p;
1010
        gchar *delim;
1011
        MimeType *mime_type;
1012
1013
        if ((fp = fopen(file, "rb")) == NULL) return NULL;
1014
1015
        debug_print("Reading %s ...\n", file);
1016
1017
        while (fgets(buf, sizeof(buf), fp) != NULL) {
1018
                p = strchr(buf, '#');
1019
                if (p) *p = '\0';
1020
                g_strstrip(buf);
1021
1022
                p = buf;
1023
                while (*p && !isspace(*p)) p++;
1024
                if (*p) {
1025
                        *p = '\0';
1026
                        p++;
1027
                }
1028
                delim = strchr(buf, '/');
1029
                if (delim == NULL) continue;
1030
                *delim = '\0';
1031
1032
                mime_type = g_new(MimeType, 1);
1033
                mime_type->type = g_strdup(buf);
1034
                mime_type->sub_type = g_strdup(delim + 1);
1035
1036
                while (*p && isspace(*p)) p++;
1037
                if (*p)
1038
                        mime_type->extension = g_strdup(p);
1039
                else
1040
                        mime_type->extension = NULL;
1041
1042
                list = g_list_append(list, mime_type);
1043
        }
1044
1045
        fclose(fp);
1046
1047
        if (!list)
1048
                g_warning("Can't read mime.types\n");
1049
1050
        return list;
1051
}
1052
1053
EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1054
{
1055
        if (!charset)
1056
                return ENC_8BIT;
1057
        else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1058
                 !strcasecmp(charset, "US-ASCII"))
1059
                return ENC_7BIT;
1060
        else if (!strcasecmp(charset, "ISO-8859-5") ||
1061
                 !strncasecmp(charset, "KOI8-", 5) ||
1062
                 !strcasecmp(charset, "Windows-1251"))
1063
                return ENC_8BIT;
1064
        else if (!strncasecmp(charset, "ISO-8859-", 9))
1065
                return ENC_QUOTED_PRINTABLE;
1066
        else
1067
                return ENC_8BIT;
1068
}
1069
1070
EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1071
{
1072
        FILE *fp;
1073
        guchar buf[BUFFSIZE];
1074
        size_t len;
1075
        size_t octet_chars = 0;
1076
        size_t total_len = 0;
1077
        gfloat octet_percentage;
1078
1079
        if ((fp = fopen(file, "rb")) == NULL) {
1080
                FILE_OP_ERROR(file, "fopen");
1081
                return ENC_UNKNOWN;
1082
        }
1083
1084
        while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1085
                guchar *p;
1086
                gint i;
1087
1088
                for (p = buf, i = 0; i < len; ++p, ++i) {
1089
                        if (*p & 0x80)
1090
                                ++octet_chars;
1091
                }
1092
                total_len += len;
1093
        }
1094
1095
        fclose(fp);
1096
1097
        if (total_len > 0)
1098
                octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1099
        else
1100
                octet_percentage = 0.0;
1101
1102
        debug_print("procmime_get_encoding_for_text_file(): "
1103
                    "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1104
                    100.0 * octet_percentage);
1105
1106
        if (octet_percentage > 0.20) {
1107
                debug_print("using BASE64\n");
1108
                return ENC_BASE64;
1109
        } else if (octet_chars > 0) {
1110
                debug_print("using quoted-printable\n");
1111
                return ENC_QUOTED_PRINTABLE;
1112
        } else {
1113
                debug_print("using 7bit\n");
1114
                return ENC_7BIT;
1115
        }
1116
}
1117
1118
const gchar *procmime_get_encoding_str(EncodingType encoding)
1119
{
1120
        static const gchar *encoding_str[] = {
1121
                "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1122
                NULL
1123
        };
1124
1125
        if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1126
                return encoding_str[encoding];
1127
        else
1128
                return NULL;
1129
}