1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 package PublicInbox::MsgIter;
8 our @EXPORT = qw(msg_iter msg_part_text);
11 # Like Email::MIME::walk_parts, but this is:
13 # * passes depth and indices to the iterator callback
16 my @parts = $mime->subparts;
19 @parts = map { [ $_, 1, ++$i ] } @parts;
20 while (my $p = shift @parts) {
21 my ($part, $depth, @idx) = @$p;
22 my @sub = $part->subparts;
26 @sub = map { [ $_, $depth, @idx, ++$i ] } @sub;
27 @parts = (@sub, @parts);
37 sub msg_part_text ($$) {
40 my $s = eval { $part->body_str };
43 # text/plain is the default, multipart/mixed happened a few
44 # times when it should not have been:
45 # <87llgalspt.fsf@free.fr>
46 # <200308111450.h7BEoOu20077@mail.osdl.org>
47 if ($ct =~ m!\btext/plain\b!i || $ct =~ m!\bmultipart/mixed\b!i) {
48 # Try to assume UTF-8 because Alpine seems to
49 # do wacky things and set charset=X-UNKNOWN
50 $part->charset_set('UTF-8');
51 $s = eval { $part->body_str };
53 # If forcing charset=UTF-8 failed,
54 # caller will warn further down...
55 $s = $part->body if $@;