]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgIter.pm
msgiter: msg_part_text returns undef on text/html
[public-inbox.git] / lib / PublicInbox / MsgIter.pm
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # read-only utilities for Email::MIME
5 package PublicInbox::MsgIter;
6 use strict;
7 use warnings;
8 use base qw(Exporter);
9 our @EXPORT = qw(msg_iter msg_part_text);
10 use PublicInbox::MIME;
11
12 # Like Email::MIME::walk_parts, but this is:
13 # * non-recursive
14 # * passes depth and indices to the iterator callback
15 sub msg_iter ($$) {
16         my ($mime, $cb) = @_;
17         my @parts = $mime->subparts;
18         if (@parts) {
19                 my $i = 0;
20                 @parts = map { [ $_, 1, ++$i ] } @parts;
21                 while (my $p = shift @parts) {
22                         my ($part, $depth, @idx) = @$p;
23                         my @sub = $part->subparts;
24                         if (@sub) {
25                                 $depth++;
26                                 $i = 0;
27                                 @sub = map { [ $_, $depth, @idx, ++$i ] } @sub;
28                                 @parts = (@sub, @parts);
29                         } else {
30                                 $cb->($p);
31                         }
32                 }
33         } else {
34                 $cb->([$mime, 0, 0]);
35         }
36 }
37
38 sub msg_part_text ($$) {
39         my ($part, $ct) = @_;
40
41         # TODO: we may offer a separate sub for people who need to index
42         # HTML-only mail, but the majority of HTML mail is multipart/alternative
43         # with a text part which we don't have to waste cycles decoding
44         return if $ct =~ m!\btext/x?html\b!;
45
46         my $s = eval { $part->body_str };
47         my $err = $@;
48
49         # text/plain is the default, multipart/mixed happened a few
50         # times when it should not have been:
51         #   <87llgalspt.fsf@free.fr>
52         #   <200308111450.h7BEoOu20077@mail.osdl.org>
53         if ($err && ($ct =~ m!\btext/\b!i ||
54                         $ct =~ m!\bmultipart/mixed\b!i)) {
55                 my $cte = $part->header_raw('Content-Transfer-Encoding');
56                 if (defined($cte) && $cte =~ /\b7bit\b/i) {
57                         $s = $part->body;
58                         $err = undef if $s =~ /\A[[:ascii:]]+\z/s;
59                 } else {
60                         # Try to assume UTF-8 because Alpine seems to
61                         # do wacky things and set charset=X-UNKNOWN
62                         $part->charset_set('UTF-8');
63                         $s = eval { $part->body_str };
64                 }
65
66                 # If forcing charset=UTF-8 failed,
67                 # caller will warn further down...
68                 $s = $part->body if $@;
69         }
70         ($s, $err);
71 }
72
73 1;