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>
4 # read-only utilities for Email::MIME
5 package PublicInbox::MsgIter;
9 our @EXPORT = qw(msg_iter msg_part_text);
10 use PublicInbox::MIME;
12 # Like Email::MIME::walk_parts, but this is:
14 # * passes depth and indices to the iterator callback
17 my @parts = $mime->subparts;
20 @parts = map { [ $_, 1, ++$i ] } @parts;
21 while (my $p = shift @parts) {
22 my ($part, $depth, @idx) = @$p;
23 my @sub = $part->subparts;
27 @sub = map { [ $_, $depth, @idx, ++$i ] } @sub;
28 @parts = (@sub, @parts);
38 sub msg_part_text ($$) {
41 my $s = eval { $part->body_str };
44 # text/plain is the default, multipart/mixed happened a few
45 # times when it should not have been:
46 # <87llgalspt.fsf@free.fr>
47 # <200308111450.h7BEoOu20077@mail.osdl.org>
48 if ($err && ($ct =~ m!\btext/\b!i ||
49 $ct =~ m!\bmultipart/mixed\b!i)) {
50 my $cte = $part->header_raw('Content-Transfer-Encoding');
51 if (defined($cte) && $cte =~ /\b7bit\b/i) {
53 $err = undef if $s =~ /\A[[:ascii:]]+\z/s;
55 # Try to assume UTF-8 because Alpine seems to
56 # do wacky things and set charset=X-UNKNOWN
57 $part->charset_set('UTF-8');
58 $s = eval { $part->body_str };
61 # If forcing charset=UTF-8 failed,
62 # caller will warn further down...
63 $s = $part->body if $@;