Statistics
| Revision:

root / src / procmime.c @ 478

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