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 ($ct =~ m!\btext/plain\b!i || $ct =~ m!\bmultipart/mixed\b!i) {
49 # Try to assume UTF-8 because Alpine seems to
50 # do wacky things and set charset=X-UNKNOWN
51 $part->charset_set('UTF-8');
52 $s = eval { $part->body_str };
54 # If forcing charset=UTF-8 failed,
55 # caller will warn further down...
56 $s = $part->body if $@;