]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Eml.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / Eml.pm
index 6f6874cd2379b145f9cbe23177e9fe651de1c2ac..462d51fc0cbcd118770abc2ba8bbe0af9fea1008 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Lazy MIME parser, it still slurps the full message but keeps short
@@ -129,7 +129,7 @@ sub new {
 sub new_sub {
        my (undef, $ref) = @_;
        # special case for messages like <85k5su9k59.fsf_-_@lola.goethe.zz>
-       $$ref =~ /\A(\r?\n)/s or goto &new;
+       $$ref =~ /\A(\r?\n)/s or return new(undef, $ref);
        my $hdr = substr($$ref, 0, $+[0], ''); # sv_chop on $$ref
        bless { hdr => \$hdr, crlf => $1, bdy => $ref }, __PACKAGE__;
 }
@@ -235,11 +235,11 @@ sub mp_descend ($$) {
 # $arg - user-supplied arg (think pthread_create)
 # $once - unref body scalar during iteration
 sub each_part {
-       my ($self, $cb, $arg, $once) = @_;
+       my ($self, $cb, $arg, $once, $all) = @_;
        my $p = mp_descend($self, $once // 0) or
-                                       return $cb->([$self, 0, 0], $arg);
+                                       return $cb->([$self, 0, 1], $arg);
 
-       $cb->([$self, 0, 0], $arg) if $self->{-call_cb}; # rare
+       $cb->([$self, 0, 0], $arg) if ($all || $self->{-call_cb}); # rare
 
        $p = [ $p, 0 ];
        my @s; # our virtual stack
@@ -255,7 +255,8 @@ sub each_part {
                                (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};
+                       ($all || $sub->{-call_cb}) and
+                               $cb->([$sub, $depth, @idx], $arg);
                } else { # a leaf node
                        $cb->([$sub, $depth, @idx], $arg);
                }
@@ -367,14 +368,19 @@ sub header_str_set {
        my ($self, $name, @vals) = @_;
        for (@vals) {
                next unless /[^\x20-\x7e]/;
-               utf8::encode($_); # to octets
                # 39: int((75 - length("Subject: =?UTF-8?B?".'?=') ) / 4) * 3;
-               s/(.{1,39})/'=?UTF-8?B?'.encode_base64($1, '').'?='/ges;
+               s/(.{1,39})/
+                       my $x = $1;
+                       utf8::encode($x); # to octets
+                       '=?UTF-8?B?'.encode_base64($x, '').'?='
+               /xges;
        }
        header_set($self, $name, @vals);
 }
 
-sub mhdr_decode ($) { eval { $MIME_Header->decode($_[0]) } // $_[0] }
+sub mhdr_decode ($) {
+       eval { $MIME_Header->decode($_[0], Encode::FB_DEFAULT) } // $_[0];
+}
 
 sub filename {
        my $dis = header_raw($_[0], 'Content-Disposition');