]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Eml.pm
nntp: smsg_range_i: favor ->{$field} lookups when possible
[public-inbox.git] / lib / PublicInbox / Eml.pm
index 2ccbb6597de406680376ad9a02a419ee53d1015b..6f6874cd2379b145f9cbe23177e9fe651de1c2ac 100644 (file)
@@ -38,9 +38,11 @@ my $MIME_Header = find_encoding('MIME-Header');
 use PublicInbox::EmlContentFoo qw(parse_content_type parse_content_disposition);
 $PublicInbox::EmlContentFoo::STRICT_PARAMS = 0;
 
-our $MAXPARTS = 1000; # same as SpamAssassin
-our $MAXDEPTH = 20; # seems enough, Perl sucks, here
-our $MAXBOUNDLEN = 2048; # same as postfix
+our $mime_parts_limit = 1000; # same as SpamAssassin (not in postfix AFAIK)
+
+# the rest of the limit names are taken from postfix:
+our $mime_nesting_limit = 20; # seems enough, Perl sucks, here
+our $mime_boundary_length_limit = 2048; # same as postfix
 our $header_size_limit = 102400; # same as postfix
 
 my %MIME_ENC = (qp => \&enc_qp, base64 => \&encode_base64);
@@ -58,6 +60,14 @@ my %DECODE_FULL = (
 our %STR_TYPE = (text => 1);
 our %STR_SUBTYPE = (plain => 1, html => 1);
 
+# message/* subtypes we descend into
+our %MESSAGE_DESCEND = (
+       news => 1, # RFC 1849 (obsolete, but archives are forever)
+       rfc822 => 1, # RFC 2046
+       rfc2822 => 1, # gmime handles this (but not rfc5322)
+       global => 1, # RFC 6532
+);
+
 my %re_memo;
 sub re_memo ($) {
        my ($k) = @_;
@@ -147,13 +157,25 @@ sub ct ($) {
 }
 
 # returns a queue of sub-parts iff it's worth descending into
-# TODO: descend into message/rfc822 parts (Email::MIME didn't)
 sub mp_descend ($$) {
        my ($self, $nr) = @_; # or $once for top-level
-       my $bnd = ct($self)->{attributes}->{boundary} // return; # single-part
-       return if $bnd eq '' || length($bnd) >= $MAXBOUNDLEN;
+       my $ct = ct($self);
+       my $type = lc($ct->{type});
+       if ($type eq 'message' && $MESSAGE_DESCEND{lc($ct->{subtype})}) {
+               my $nxt = new(undef, body_raw($self));
+               $self->{-call_cb} = $nxt->{is_submsg} = 1;
+               return [ $nxt ];
+       }
+       return if $type ne 'multipart';
+       my $bnd = $ct->{attributes}->{boundary} // return; # single-part
+       return if $bnd eq '' || length($bnd) >= $mime_boundary_length_limit;
        $bnd = quotemeta($bnd);
 
+       # this is a multipart message that didn't get descended into in
+       # public-inbox <= 1.5.0, so ensure we call the user callback for
+       # this part to not break PSGI downloads.
+       $self->{-call_cb} = $self->{is_submsg};
+
        # "multipart" messages can exist w/o a body
        my $bdy = ($nr ? delete($self->{bdy}) : \(body_raw($self))) or return;
 
@@ -179,7 +201,7 @@ sub mp_descend ($$) {
                                # + 3 since we don't want the last part
                                # processed to include any other excluded
                                # parts ($nr starts at 1, and I suck at math)
-                               $MAXPARTS + 3 - $nr);
+                               $mime_parts_limit + 3 - $nr);
 
        if (@parts) { # the usual path if we got this far:
                undef $bdy; # release memory ASAP if $nr > 0
@@ -187,14 +209,15 @@ sub mp_descend ($$) {
                # compatibility with Email::MIME
                $parts[-1] =~ s/\n\r?\n\z/\n/s if $epilogue_missing;
 
-               @parts = grep /[^ \t\r\n]/s, @parts; # ignore empty parts
+               # ignore empty parts
+               @parts = map { new_sub(undef, \$_) } grep /[^ \t\r\n]/s, @parts;
 
                # Keep "From: someone..." from preamble in old,
                # buggy versions of git-send-email, otherwise drop it
                # There's also a case where quoted text showed up in the
                # preamble
                # <20060515162817.65F0F1BBAE@citi.umich.edu>
-               unshift(@parts, $pre) if $pre =~ /:/s;
+               unshift(@parts, new_sub(undef, \$pre)) if $pre =~ /:/s;
                return \@parts;
        }
        # "multipart", but no boundary found, treat as single part
@@ -215,18 +238,24 @@ sub each_part {
        my ($self, $cb, $arg, $once) = @_;
        my $p = mp_descend($self, $once // 0) or
                                        return $cb->([$self, 0, 0], $arg);
+
+       $cb->([$self, 0, 0], $arg) if $self->{-call_cb}; # rare
+
        $p = [ $p, 0 ];
        my @s; # our virtual stack
        my $nr = 0;
-       while ((scalar(@{$p->[0]}) || ($p = pop @s)) && ++$nr <= $MAXPARTS) {
+       while ((scalar(@{$p->[0]}) || ($p = pop @s)) &&
+                       ++$nr <= $mime_parts_limit) {
                ++$p->[-1]; # bump index
                my (undef, @idx) = @$p;
                @idx = (join('.', @idx));
                my $depth = ($idx[0] =~ tr/././) + 1;
-               my $sub = new_sub(undef, \(shift @{$p->[0]}));
-               if ($depth < $MAXDEPTH && (my $nxt = mp_descend($sub, $nr))) {
+               my $sub = shift @{$p->[0]};
+               if ($depth < $mime_nesting_limit &&
+                               (my $nxt = mp_descend($sub, $nr))) {
                        push(@s, $p) if scalar @{$p->[0]};
                        $p = [ $nxt, @idx, 0 ];
+                       $cb->([$sub, $depth, @idx], $arg) if $sub->{-call_cb};
                } else { # a leaf node
                        $cb->([$sub, $depth, @idx], $arg);
                }
@@ -266,7 +295,7 @@ sub subparts {
        if ($$bdy =~ /^--\Q$bnd\E--[ \t]*\r?\n(.+)\z/sm) {
                $self->{epilogue} = $1;
        }
-       map { new_sub(undef, \$_) } @$parts;
+       @$parts;
 }
 
 sub parts_set {