Statistics
| Revision:

root / src / procmime.c @ 1

History | View | Annotate | Download (25.5 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 <stdio.h>
28
#include <string.h>
29
#include <locale.h>
30
#include <ctype.h>
31

    
32
#include "intl.h"
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
        if (conv_get_locale_charset() == C_EUC_JP &&
353
            strchr(content_type, '\033')) {
354
                gint len;
355
                len = strlen(content_type) * 2 + 1;
356
                Xalloca(buf, len, return);
357
                conv_jistoeuc(buf, len, content_type);
358
        } else
359
                Xstrdup_a(buf, content_type, return);
360

    
361
        g_free(mimeinfo->content_type);
362
        g_free(mimeinfo->charset);
363
        g_free(mimeinfo->name);
364
        mimeinfo->content_type = NULL;
365
        mimeinfo->charset      = NULL;
366
        mimeinfo->name         = NULL;
367

    
368
        if ((delim = strchr(buf, ';'))) *delim = '\0';
369
        mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
370

    
371
        mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
372

    
373
        if (!delim) return;
374
        p = delim + 1;
375

    
376
        for (;;) {
377
                gchar *eq;
378
                gchar *attr, *value;
379

    
380
                if ((delim = strchr(p, ';'))) *delim = '\0';
381

    
382
                if (!(eq = strchr(p, '='))) break;
383

    
384
                *eq = '\0';
385
                attr = p;
386
                g_strstrip(attr);
387
                value = eq + 1;
388
                g_strstrip(value);
389

    
390
                if (*value == '"')
391
                        extract_quote(value, '"');
392
                else {
393
                        eliminate_parenthesis(value, '(', ')');
394
                        g_strstrip(value);
395
                }
396

    
397
                if (*value) {
398
                        if (!strcasecmp(attr, "charset"))
399
                                mimeinfo->charset = g_strdup(value);
400
                        else if (!strcasecmp(attr, "name")) {
401
                                gchar *tmp;
402
                                size_t len;
403

    
404
                                len = strlen(value) + 1;
405
                                Xalloca(tmp, len, return);
406
                                conv_unmime_header(tmp, len, value, NULL);
407
                                mimeinfo->name = g_strdup(tmp);
408
                        } else if (!strcasecmp(attr, "boundary"))
409
                                mimeinfo->boundary = g_strdup(value);
410
                }
411

    
412
                if (!delim) break;
413
                p = delim + 1;
414
        }
415

    
416
        if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
417
                mimeinfo->mime_type = MIME_TEXT;
418
}
419

    
420
void procmime_scan_content_disposition(MimeInfo *mimeinfo,
421
                                       const gchar *content_disposition)
422
{
423
        gchar *delim, *p, *dispos;
424
        gchar *buf;
425

    
426
        if (conv_get_locale_charset() == C_EUC_JP &&
427
            strchr(content_disposition, '\033')) {
428
                gint len;
429
                len = strlen(content_disposition) * 2 + 1;
430
                Xalloca(buf, len, return);
431
                conv_jistoeuc(buf, len, content_disposition);
432
        } else
433
                Xstrdup_a(buf, content_disposition, return);
434

    
435
        if ((delim = strchr(buf, ';'))) *delim = '\0';
436
        mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
437

    
438
        if (!delim) return;
439
        p = delim + 1;
440

    
441
        for (;;) {
442
                gchar *eq;
443
                gchar *attr, *value;
444

    
445
                if ((delim = strchr(p, ';'))) *delim = '\0';
446

    
447
                if (!(eq = strchr(p, '='))) break;
448

    
449
                *eq = '\0';
450
                attr = p;
451
                g_strstrip(attr);
452
                value = eq + 1;
453
                g_strstrip(value);
454

    
455
                if (*value == '"')
456
                        extract_quote(value, '"');
457
                else {
458
                        eliminate_parenthesis(value, '(', ')');
459
                        g_strstrip(value);
460
                }
461

    
462
                if (*value) {
463
                        if (!strcasecmp(attr, "filename")) {
464
                                gchar *tmp;
465
                                size_t len;
466

    
467
                                len = strlen(value) + 1;
468
                                Xalloca(tmp, len, return);
469
                                conv_unmime_header(tmp, len, value, NULL);
470
                                g_free(mimeinfo->filename);
471
                                mimeinfo->filename = g_strdup(tmp);
472
                                break;
473
                        }
474
                }
475

    
476
                if (!delim) break;
477
                p = delim + 1;
478
        }
479
}
480

    
481
enum
482
{
483
        H_CONTENT_TRANSFER_ENCODING = 0,
484
        H_CONTENT_TYPE                    = 1,
485
        H_CONTENT_DISPOSITION            = 2
486
};
487

    
488
MimeInfo *procmime_scan_mime_header(FILE *fp)
489
{
490
        static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
491
                                                          NULL, FALSE},
492
                                       {"Content-Type:", NULL, TRUE},
493
                                       {"Content-Disposition:",
494
                                                          NULL, TRUE},
495
                                       {NULL,                  NULL, FALSE}};
496
        gchar buf[BUFFSIZE];
497
        gint hnum;
498
        HeaderEntry *hp;
499
        MimeInfo *mimeinfo;
500

    
501
        g_return_val_if_fail(fp != NULL, NULL);
502

    
503
        mimeinfo = procmime_mimeinfo_new();
504
        mimeinfo->mime_type = MIME_TEXT;
505
        mimeinfo->encoding_type = ENC_7BIT;
506
        mimeinfo->fpos = ftell(fp);
507

    
508
        while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
509
               != -1) {
510
                hp = hentry + hnum;
511

    
512
                if (H_CONTENT_TRANSFER_ENCODING == hnum) {
513
                        procmime_scan_encoding
514
                                (mimeinfo, buf + strlen(hp->name));
515
                } else if (H_CONTENT_TYPE == hnum) {
516
                        procmime_scan_content_type
517
                                (mimeinfo, buf + strlen(hp->name));
518
                } else if (H_CONTENT_DISPOSITION == hnum) {
519
                        procmime_scan_content_disposition
520
                                (mimeinfo, buf + strlen(hp->name));
521
                }
522
        }
523

    
524
        if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
525
            mimeinfo->name) {
526
                const gchar *type;
527
                type = procmime_get_mime_type(mimeinfo->name);
528
                if (type)
529
                        mimeinfo->mime_type = procmime_scan_mime_type(type);
530
        }
531

    
532
        if (!mimeinfo->content_type)
533
                mimeinfo->content_type = g_strdup("text/plain");
534

    
535
        return mimeinfo;
536
}
537

    
538
FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
539
{
540
        gchar buf[BUFFSIZE];
541
        gchar *boundary = NULL;
542
        gint boundary_len = 0;
543
        gboolean tmp_file = FALSE;
544

    
545
        g_return_val_if_fail(infp != NULL, NULL);
546
        g_return_val_if_fail(mimeinfo != NULL, NULL);
547

    
548
        if (!outfp) {
549
                outfp = my_tmpfile();
550
                if (!outfp) {
551
                        perror("tmpfile");
552
                        return NULL;
553
                }
554
                tmp_file = TRUE;
555
        }
556

    
557
        if (mimeinfo->parent && mimeinfo->parent->boundary) {
558
                boundary = mimeinfo->parent->boundary;
559
                boundary_len = strlen(boundary);
560
        }
561

    
562
        if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
563
                while (fgets(buf, sizeof(buf), infp) != NULL &&
564
                       (!boundary ||
565
                        !IS_BOUNDARY(buf, boundary, boundary_len))) {
566
                        gint len;
567
                        len = qp_decode_line(buf);
568
                        fwrite(buf, len, 1, outfp);
569
                }
570
        } else if (mimeinfo->encoding_type == ENC_BASE64) {
571
                gchar outbuf[BUFFSIZE];
572
                gint len;
573
                Base64Decoder *decoder;
574
                gboolean uncanonicalize = FALSE;
575
                FILE *tmpfp = outfp;
576

    
577
                if (mimeinfo->mime_type == MIME_TEXT ||
578
                    mimeinfo->mime_type == MIME_TEXT_HTML ||
579
                    mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
580
                        uncanonicalize = TRUE;
581
                        tmpfp = my_tmpfile();
582
                        if (!tmpfp) {
583
                                perror("tmpfile");
584
                                if (tmp_file) fclose(outfp);
585
                                return NULL;
586
                        }
587
                }
588

    
589
                decoder = base64_decoder_new();
590
                while (fgets(buf, sizeof(buf), infp) != NULL &&
591
                       (!boundary ||
592
                        !IS_BOUNDARY(buf, boundary, boundary_len))) {
593
                        len = base64_decoder_decode(decoder, buf, outbuf);
594
                        if (len < 0) {
595
                                g_warning("Bad BASE64 content\n");
596
                                break;
597
                        }
598
                        fwrite(outbuf, sizeof(gchar), len, tmpfp);
599
                }
600
                base64_decoder_free(decoder);
601

    
602
                if (uncanonicalize) {
603
                        rewind(tmpfp);
604
                        while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
605
                                strcrchomp(buf);
606
                                fputs(buf, outfp);
607
                        }
608
                        fclose(tmpfp);
609
                }
610
        } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
611
                gchar outbuf[BUFFSIZE];
612
                gint len;
613
                gboolean flag = FALSE;
614

    
615
                while (fgets(buf, sizeof(buf), infp) != NULL &&
616
                       (!boundary ||
617
                        !IS_BOUNDARY(buf, boundary, boundary_len))) {
618
                        if(!flag && strncmp(buf,"begin ", 6)) continue;
619

    
620
                        if (flag) {
621
                                len = fromuutobits(outbuf, buf);
622
                                if (len <= 0) {
623
                                        if (len < 0) 
624
                                                g_warning("Bad UUENCODE content(%d)\n", len);
625
                                        break;
626
                                }
627
                                fwrite(outbuf, sizeof(gchar), len, outfp);
628
                        } else
629
                                flag = TRUE;
630
                }
631
        } else {
632
                while (fgets(buf, sizeof(buf), infp) != NULL &&
633
                       (!boundary ||
634
                        !IS_BOUNDARY(buf, boundary, boundary_len))) {
635
                        fputs(buf, outfp);
636
                }
637
        }
638

    
639
        if (tmp_file) rewind(outfp);
640
        return outfp;
641
}
642

    
643
gint procmime_get_part(const gchar *outfile, const gchar *infile,
644
                       MimeInfo *mimeinfo)
645
{
646
        FILE *infp, *outfp;
647
        gchar buf[BUFFSIZE];
648

    
649
        g_return_val_if_fail(outfile != NULL, -1);
650
        g_return_val_if_fail(infile != NULL, -1);
651
        g_return_val_if_fail(mimeinfo != NULL, -1);
652

    
653
        if ((infp = fopen(infile, "rb")) == NULL) {
654
                FILE_OP_ERROR(infile, "fopen");
655
                return -1;
656
        }
657
        if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
658
                FILE_OP_ERROR(infile, "fseek");
659
                fclose(infp);
660
                return -1;
661
        }
662
        if ((outfp = fopen(outfile, "wb")) == NULL) {
663
                FILE_OP_ERROR(outfile, "fopen");
664
                fclose(infp);
665
                return -1;
666
        }
667

    
668
        while (fgets(buf, sizeof(buf), infp) != NULL)
669
                if (buf[0] == '\r' || buf[0] == '\n') break;
670

    
671
        procmime_decode_content(outfp, infp, mimeinfo);
672

    
673
        fclose(infp);
674
        if (fclose(outfp) == EOF) {
675
                FILE_OP_ERROR(outfile, "fclose");
676
                unlink(outfile);
677
                return -1;
678
        }
679

    
680
        return 0;
681
}
682

    
683
FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
684
{
685
        FILE *tmpfp, *outfp;
686
        gchar *src_codeset;
687
        gboolean conv_fail = FALSE;
688
        gchar buf[BUFFSIZE];
689
        gchar *str;
690

    
691
        g_return_val_if_fail(mimeinfo != NULL, NULL);
692
        g_return_val_if_fail(infp != NULL, NULL);
693
        g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
694
                             mimeinfo->mime_type == MIME_TEXT_HTML, NULL);
695

    
696
        if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
697
                perror("fseek");
698
                return NULL;
699
        }
700

    
701
        while (fgets(buf, sizeof(buf), infp) != NULL)
702
                if (buf[0] == '\r' || buf[0] == '\n') break;
703

    
704
        tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
705
        if (!tmpfp)
706
                return NULL;
707

    
708
        if ((outfp = my_tmpfile()) == NULL) {
709
                perror("tmpfile");
710
                fclose(tmpfp);
711
                return NULL;
712
        }
713

    
714
        src_codeset = prefs_common.force_charset
715
                ? prefs_common.force_charset : mimeinfo->charset;
716

    
717
        if (mimeinfo->mime_type == MIME_TEXT) {
718
                while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
719
                        str = conv_codeset_strdup(buf, src_codeset, NULL);
720
                        if (str) {
721
                                fputs(str, outfp);
722
                                g_free(str);
723
                        } else {
724
                                conv_fail = TRUE;
725
                                fputs(buf, outfp);
726
                        }
727
                }
728
        } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
729
                HTMLParser *parser;
730
                CodeConverter *conv;
731

    
732
                conv = conv_code_converter_new(src_codeset);
733
                parser = html_parser_new(tmpfp, conv);
734
                while ((str = html_parse(parser)) != NULL) {
735
                        fputs(str, outfp);
736
                }
737
                html_parser_destroy(parser);
738
                conv_code_converter_destroy(conv);
739
        }
740

    
741
        if (conv_fail)
742
                g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
743

    
744
        fclose(tmpfp);
745
        rewind(outfp);
746

    
747
        return outfp;
748
}
749

    
750
/* search the first text part of (multipart) MIME message,
751
   decode, convert it and output to outfp. */
752
FILE *procmime_get_first_text_content(MsgInfo *msginfo)
753
{
754
        FILE *infp, *outfp = NULL;
755
        MimeInfo *mimeinfo, *partinfo;
756

    
757
        g_return_val_if_fail(msginfo != NULL, NULL);
758

    
759
        mimeinfo = procmime_scan_message(msginfo);
760
        if (!mimeinfo) return NULL;
761

    
762
        if ((infp = procmsg_open_message(msginfo)) == NULL) {
763
                procmime_mimeinfo_free_all(mimeinfo);
764
                return NULL;
765
        }
766

    
767
        partinfo = mimeinfo;
768
        while (partinfo && partinfo->mime_type != MIME_TEXT)
769
                partinfo = procmime_mimeinfo_next(partinfo);
770
        if (!partinfo) {
771
                partinfo = mimeinfo;
772
                while (partinfo && partinfo->mime_type != MIME_TEXT_HTML)
773
                        partinfo = procmime_mimeinfo_next(partinfo);
774
        }
775

    
776
        if (partinfo)
777
                outfp = procmime_get_text_content(partinfo, infp);
778

    
779
        fclose(infp);
780
        procmime_mimeinfo_free_all(mimeinfo);
781

    
782
        return outfp;
783
}
784

    
785
gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
786
                                   const gchar *str, StrFindFunc find_func)
787
{
788

    
789
        FILE *infp, *outfp;
790
        gchar buf[BUFFSIZE];
791

    
792
        g_return_val_if_fail(mimeinfo != NULL, FALSE);
793
        g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
794
                             mimeinfo->mime_type == MIME_TEXT_HTML, FALSE);
795
        g_return_val_if_fail(str != NULL, FALSE);
796
        g_return_val_if_fail(find_func != NULL, FALSE);
797

    
798
        if ((infp = fopen(filename, "rb")) == NULL) {
799
                FILE_OP_ERROR(filename, "fopen");
800
                return FALSE;
801
        }
802

    
803
        outfp = procmime_get_text_content(mimeinfo, infp);
804
        fclose(infp);
805

    
806
        if (!outfp)
807
                return FALSE;
808

    
809
        while (fgets(buf, sizeof(buf), outfp) != NULL) {
810
                strretchomp(buf);
811
                if (find_func(buf, str)) {
812
                        fclose(outfp);
813
                        return TRUE;
814
                }
815
        }
816

    
817
        fclose(outfp);
818

    
819
        return FALSE;
820
}
821

    
822
gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
823
                              StrFindFunc find_func)
824
{
825
        MimeInfo *mimeinfo;
826
        MimeInfo *partinfo;
827
        gchar *filename;
828
        gboolean found = FALSE;
829

    
830
        g_return_val_if_fail(msginfo != NULL, FALSE);
831
        g_return_val_if_fail(str != NULL, FALSE);
832
        g_return_val_if_fail(find_func != NULL, FALSE);
833

    
834
        filename = procmsg_get_message_file(msginfo);
835
        if (!filename) return FALSE;
836
        mimeinfo = procmime_scan_message(msginfo);
837

    
838
        for (partinfo = mimeinfo; partinfo != NULL;
839
             partinfo = procmime_mimeinfo_next(partinfo)) {
840
                if (partinfo->mime_type == MIME_TEXT ||
841
                    partinfo->mime_type == MIME_TEXT_HTML) {
842
                        if (procmime_find_string_part
843
                                (partinfo, filename, str, find_func) == TRUE) {
844
                                found = TRUE;
845
                                break;
846
                        }
847
                }
848
        }
849

    
850
        procmime_mimeinfo_free_all(mimeinfo);
851
        g_free(filename);
852

    
853
        return found;
854
}
855

    
856
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
857
{
858
        static guint32 id = 0;
859
        gchar *base;
860
        gchar *filename;
861
        gchar f_prefix[10];
862

    
863
        g_return_val_if_fail(mimeinfo != NULL, NULL);
864

    
865
        g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
866

    
867
        if (MIME_TEXT_HTML == mimeinfo->mime_type)
868
                base = "mimetmp.html";
869
        else {
870
                const gchar *base_;
871
                base_ = mimeinfo->filename ? mimeinfo->filename
872
                        : mimeinfo->name ? mimeinfo->name : "mimetmp";
873
                base_ = g_basename(base_);
874
                if (*base_ == '\0') base_ = "mimetmp";
875
                Xstrdup_a(base, base_, return NULL);
876
                subst_for_filename(base);
877
        }
878

    
879
        filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
880
                               f_prefix, base, NULL);
881

    
882
        return filename;
883
}
884

    
885
ContentType procmime_scan_mime_type(const gchar *mime_type)
886
{
887
        ContentType type;
888

    
889
        if (!strncasecmp(mime_type, "text/html", 9))
890
                type = MIME_TEXT_HTML;
891
        else if (!strncasecmp(mime_type, "text/", 5))
892
                type = MIME_TEXT;
893
        else if (!strncasecmp(mime_type, "message/rfc822", 14))
894
                type = MIME_MESSAGE_RFC822;
895
        else if (!strncasecmp(mime_type, "message/", 8))
896
                type = MIME_TEXT;
897
        else if (!strncasecmp(mime_type, "application/octet-stream", 24))
898
                type = MIME_APPLICATION_OCTET_STREAM;
899
        else if (!strncasecmp(mime_type, "application/", 12))
900
                type = MIME_APPLICATION;
901
        else if (!strncasecmp(mime_type, "multipart/", 10))
902
                type = MIME_MULTIPART;
903
        else if (!strncasecmp(mime_type, "image/", 6))
904
                type = MIME_IMAGE;
905
        else if (!strncasecmp(mime_type, "audio/", 6))
906
                type = MIME_AUDIO;
907
        else if (!strcasecmp(mime_type, "text"))
908
                type = MIME_TEXT;
909
        else
910
                type = MIME_UNKNOWN;
911

    
912
        return type;
913
}
914

    
915
static GList *mime_type_list = NULL;
916

    
917
gchar *procmime_get_mime_type(const gchar *filename)
918
{
919
        static GHashTable *mime_type_table = NULL;
920
        MimeType *mime_type;
921
        const gchar *p;
922
        gchar *ext;
923

    
924
        if (!mime_type_table) {
925
                mime_type_table = procmime_get_mime_type_table();
926
                if (!mime_type_table) return NULL;
927
        }
928

    
929
        filename = g_basename(filename);
930
        p = strrchr(filename, '.');
931
        if (!p) return NULL;
932

    
933
        Xstrdup_a(ext, p + 1, return NULL);
934
        g_strdown(ext);
935
        mime_type = g_hash_table_lookup(mime_type_table, ext);
936
        if (mime_type) {
937
                gchar *str;
938

    
939
                str = g_strconcat(mime_type->type, "/", mime_type->sub_type,
940
                                  NULL);
941
                return str;
942
        }
943

    
944
        return NULL;
945
}
946

    
947
static GHashTable *procmime_get_mime_type_table(void)
948
{
949
        GHashTable *table = NULL;
950
        GList *cur;
951
        MimeType *mime_type;
952
        gchar **exts;
953

    
954
        if (!mime_type_list) {
955
                GList *list;
956
                gchar *dir;
957

    
958
                mime_type_list =
959
                        procmime_get_mime_type_list(SYSCONFDIR "/mime.types");
960
                if (!mime_type_list) {
961
                        list = procmime_get_mime_type_list("/etc/mime.types");
962
                        mime_type_list = g_list_concat(mime_type_list, list);
963
                }
964
                dir = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, RC_DIR,
965
                                  G_DIR_SEPARATOR_S, "mime.types", NULL);
966
                list = procmime_get_mime_type_list(dir);
967
                g_free(dir);
968
                mime_type_list = g_list_concat(mime_type_list, list);
969

    
970
                if (!mime_type_list) {
971
                        g_warning("mime.types not found\n");
972
                        return NULL;
973
                }
974
        }
975

    
976
        table = g_hash_table_new(g_str_hash, g_str_equal);
977

    
978
        for (cur = mime_type_list; cur != NULL; cur = cur->next) {
979
                gint i;
980
                gchar *key;
981

    
982
                mime_type = (MimeType *)cur->data;
983

    
984
                if (!mime_type->extension) continue;
985

    
986
                exts = g_strsplit(mime_type->extension, " ", 16);
987
                for (i = 0; exts[i] != NULL; i++) {
988
                        /* make the key case insensitive */
989
                        g_strdown(exts[i]);
990
                        /* use previously dup'd key on overwriting */
991
                        if (g_hash_table_lookup(table, exts[i]))
992
                                key = exts[i];
993
                        else
994
                                key = g_strdup(exts[i]);
995
                        g_hash_table_insert(table, key, mime_type);
996
                }
997
                g_strfreev(exts);
998
        }
999

    
1000
        return table;
1001
}
1002

    
1003
static GList *procmime_get_mime_type_list(const gchar *file)
1004
{
1005
        GList *list = NULL;
1006
        FILE *fp;
1007
        gchar buf[BUFFSIZE];
1008
        guchar *p;
1009
        gchar *delim;
1010
        MimeType *mime_type;
1011

    
1012
        if ((fp = fopen(file, "rb")) == NULL) return NULL;
1013

    
1014
        debug_print("Reading %s ...\n", file);
1015

    
1016
        while (fgets(buf, sizeof(buf), fp) != NULL) {
1017
                p = strchr(buf, '#');
1018
                if (p) *p = '\0';
1019
                g_strstrip(buf);
1020

    
1021
                p = buf;
1022
                while (*p && !isspace(*p)) p++;
1023
                if (*p) {
1024
                        *p = '\0';
1025
                        p++;
1026
                }
1027
                delim = strchr(buf, '/');
1028
                if (delim == NULL) continue;
1029
                *delim = '\0';
1030

    
1031
                mime_type = g_new(MimeType, 1);
1032
                mime_type->type = g_strdup(buf);
1033
                mime_type->sub_type = g_strdup(delim + 1);
1034

    
1035
                while (*p && isspace(*p)) p++;
1036
                if (*p)
1037
                        mime_type->extension = g_strdup(p);
1038
                else
1039
                        mime_type->extension = NULL;
1040

    
1041
                list = g_list_append(list, mime_type);
1042
        }
1043

    
1044
        fclose(fp);
1045

    
1046
        if (!list)
1047
                g_warning("Can't read mime.types\n");
1048

    
1049
        return list;
1050
}
1051

    
1052
EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1053
{
1054
        if (!charset)
1055
                return ENC_8BIT;
1056
        else if (!strncasecmp(charset, "ISO-2022-", 9) ||
1057
                 !strcasecmp(charset, "US-ASCII"))
1058
                return ENC_7BIT;
1059
        else if (!strcasecmp(charset, "ISO-8859-5") ||
1060
                 !strncasecmp(charset, "KOI8-", 5) ||
1061
                 !strcasecmp(charset, "Windows-1251"))
1062
                return ENC_8BIT;
1063
        else if (!strncasecmp(charset, "ISO-8859-", 9))
1064
                return ENC_QUOTED_PRINTABLE;
1065
        else
1066
                return ENC_8BIT;
1067
}
1068

    
1069
EncodingType procmime_get_encoding_for_text_file(const gchar *file)
1070
{
1071
        FILE *fp;
1072
        guchar buf[BUFFSIZE];
1073
        size_t len;
1074
        size_t octet_chars = 0;
1075
        size_t total_len = 0;
1076
        gfloat octet_percentage;
1077

    
1078
        if ((fp = fopen(file, "rb")) == NULL) {
1079
                FILE_OP_ERROR(file, "fopen");
1080
                return ENC_UNKNOWN;
1081
        }
1082

    
1083
        while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
1084
                guchar *p;
1085
                gint i;
1086

    
1087
                for (p = buf, i = 0; i < len; ++p, ++i) {
1088
                        if (*p & 0x80)
1089
                                ++octet_chars;
1090
                }
1091
                total_len += len;
1092
        }
1093

    
1094
        fclose(fp);
1095

    
1096
        if (total_len > 0)
1097
                octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
1098
        else
1099
                octet_percentage = 0.0;
1100

    
1101
        debug_print("procmime_get_encoding_for_text_file(): "
1102
                    "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
1103
                    100.0 * octet_percentage);
1104

    
1105
        if (octet_percentage > 0.20) {
1106
                debug_print("using BASE64\n");
1107
                return ENC_BASE64;
1108
        } else if (octet_chars > 0) {
1109
                debug_print("using quoted-printable\n");
1110
                return ENC_QUOTED_PRINTABLE;
1111
        } else {
1112
                debug_print("using 7bit\n");
1113
                return ENC_7BIT;
1114
        }
1115
}
1116

    
1117
const gchar *procmime_get_encoding_str(EncodingType encoding)
1118
{
1119
        static const gchar *encoding_str[] = {
1120
                "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
1121
                NULL
1122
        };
1123

    
1124
        if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
1125
                return encoding_str[encoding];
1126
        else
1127
                return NULL;
1128
}