]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MsgIter.pm
msg_iter: make ->each_part method for PublicInbox::MIME
[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
11 # This becomes PublicInbox::MIME->each_part:
12 # Like Email::MIME::walk_parts, but this is:
13 # * non-recursive
14 # * passes depth and indices to the iterator callback
15 sub em_each_part ($$;$$) {
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 # Use this when we may accept Email::MIME from user scripts
40 # (not just PublicInbox::MIME)
41 sub msg_iter ($$;$$) { # $_[0] = PublicInbox::MIME/Email::MIME-like obj
42         my (undef, $cb, $cb_arg, $once) = @_;
43         if (my $ep = $_[0]->can('each_part')) { # PublicInbox::{MIME,*}
44                 $ep->($_[0], $cb, $cb_arg, $once);
45         } else { # for compatibility with existing Email::MIME users:
46                 em_each_part($_[0], $cb, $cb_arg, $once);
47         }
48 }
49
50 sub msg_part_text ($$) {
51         my ($part, $ct) = @_;
52
53         # TODO: we may offer a separate sub for people who need to index
54         # HTML-only mail, but the majority of HTML mail is multipart/alternative
55         # with a text part which we don't have to waste cycles decoding
56         return if $ct =~ m!\btext/x?html\b!;
57
58         my $s = eval { $part->body_str };
59         my $err = $@;
60
61         # text/plain is the default, multipart/mixed happened a few
62         # times when it should not have been:
63         #   <87llgalspt.fsf@free.fr>
64         #   <200308111450.h7BEoOu20077@mail.osdl.org>
65         if ($err && ($ct =~ m!\btext/\b!i ||
66                         $ct =~ m!\bmultipart/mixed\b!i)) {
67                 my $cte = $part->header_raw('Content-Transfer-Encoding');
68                 if (defined($cte) && $cte =~ /\b7bit\b/i) {
69                         $s = $part->body;
70                         $err = undef if $s =~ /\A[[:ascii:]]+\z/s;
71                 } else {
72                         # Try to assume UTF-8 because Alpine seems to
73                         # do wacky things and set charset=X-UNKNOWN
74                         $part->charset_set('UTF-8');
75                         $s = eval { $part->body_str };
76                 }
77
78                 # If forcing charset=UTF-8 failed,
79                 # caller will warn further down...
80                 $s = $part->body if $@;
81         }
82         ($s, $err);
83 }
84
85 # returns an array of quoted or unquoted sections
86 sub split_quotes {
87         # Quiet "Complex regular subexpression recursion limit" warning
88         # in case an inconsiderate sender quotes 32K of text at once.
89         # The warning from Perl is harmless for us since our callers can
90         # tolerate less-than-ideal matches which work within Perl limits.
91         no warnings 'regexp';
92         split(/((?:^>[^\n]*\n)+)/sm, shift);
93 }
94
95 1;