]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgIter.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / MsgIter.pm
1 # Copyright (C) 2016-2020 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, $cb_arg, $do_undef) = @_;
17         my @parts = $mime->subparts;
18         if (@parts) {
19                 $mime = $_[0] = undef if $do_undef; # saves some memory
20                 my $i = 0;
21                 @parts = map { [ $_, 1, ++$i ] } @parts;
22                 while (my $p = shift @parts) {
23                         my ($part, $depth, @idx) = @$p;
24                         my @sub = $part->subparts;
25                         if (@sub) {
26                                 $depth++;
27                                 $i = 0;
28                                 @sub = map { [ $_, $depth, @idx, ++$i ] } @sub;
29                                 @parts = (@sub, @parts);
30                         } else {
31                                 $cb->($p, $cb_arg);
32                         }
33                 }
34         } else {
35                 $cb->([$mime, 0, 0], $cb_arg);
36         }
37 }
38
39 sub msg_part_text ($$) {
40         my ($part, $ct) = @_;
41
42         # TODO: we may offer a separate sub for people who need to index
43         # HTML-only mail, but the majority of HTML mail is multipart/alternative
44         # with a text part which we don't have to waste cycles decoding
45         return if $ct =~ m!\btext/x?html\b!;
46
47         my $s = eval { $part->body_str };
48         my $err = $@;
49
50         # text/plain is the default, multipart/mixed happened a few
51         # times when it should not have been:
52         #   <87llgalspt.fsf@free.fr>
53         #   <200308111450.h7BEoOu20077@mail.osdl.org>
54         if ($err && ($ct =~ m!\btext/\b!i ||
55                         $ct =~ m!\bmultipart/mixed\b!i)) {
56                 my $cte = $part->header_raw('Content-Transfer-Encoding');
57                 if (defined($cte) && $cte =~ /\b7bit\b/i) {
58                         $s = $part->body;
59                         $err = undef if $s =~ /\A[[:ascii:]]+\z/s;
60                 } else {
61                         # Try to assume UTF-8 because Alpine seems to
62                         # do wacky things and set charset=X-UNKNOWN
63                         $part->charset_set('UTF-8');
64                         $s = eval { $part->body_str };
65                 }
66
67                 # If forcing charset=UTF-8 failed,
68                 # caller will warn further down...
69                 $s = $part->body if $@;
70         }
71         ($s, $err);
72 }
73
74 1;