Statistics
| Revision:

root / src / procmime.c @ 92

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