Statistics
| Revision:

root / src / procmime.c @ 427

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