]> Sergey Matveev's repositories - public-inbox.git/commitdiff
eml: keep body if no headers are found
authorEric Wong <e@80x24.org>
Tue, 26 Oct 2021 10:35:51 +0000 (10:35 +0000)
committerEric Wong <e@80x24.org>
Tue, 26 Oct 2021 17:43:52 +0000 (17:43 +0000)
This easily allows us to treat "git diff" output as header-less
"messages" for commands such as "lei p2q".

lib/PublicInbox/Eml.pm
t/eml.t
t/mbox_reader.t

index 3c681ba5bc2eb6efc6b0252b67a26ebd2f3b6904..485f637a3e7b41a9117cb37a9acfd8e7649dadfa 100644 (file)
@@ -122,9 +122,10 @@ sub new {
                my $hdr = substr($$ref, 0, $header_size_limit + 1);
                hdr_truncate($hdr) if length($hdr) > $header_size_limit;
                bless { hdr => \$hdr, crlf => $1 }, __PACKAGE__;
-       } else { # nothing useful
-               my $hdr = $$ref = '';
-               bless { hdr => \$hdr, crlf => "\n" }, __PACKAGE__;
+       } else { # just a body w/o header?
+               my $hdr = '';
+               my $eol = ($$ref =~ /(\r?\n)/) ? $1 : "\n";
+               bless { hdr => \$hdr, crlf => $eol, bdy => $ref }, __PACKAGE__;
        }
 }
 
diff --git a/t/eml.t b/t/eml.t
index 0cf48f225a45c50d0bb70e10acd8274525240c08..2d8993a51075f317ce11f5cc61012d55211663d6 100644 (file)
--- a/t/eml.t
+++ b/t/eml.t
@@ -216,6 +216,17 @@ if ('one newline before headers') {
        is($eml->body, "");
 }
 
+if ('body only') {
+       my $str = <<EOM;
+--- a/lib/PublicInbox/Eml.pm
++++ b/lib/PublicInbox/Eml.pm
+@@ -122,9 +122,10 @@ sub new {
+\x20
+EOM
+       my $eml = PublicInbox::Eml->new($str);
+       is($eml->body, $str, 'body-only accepted');
+}
+
 for my $cls (@classes) { # XXX: matching E::M, but not sure about this
        my $s = <<EOF;
 Content-Type: multipart/mixed; boundary="b"
index e5f57d7bcba3d177b6460cf1e7f1eee405d672aa..87e8f397662cd058aafc0075a23d4377e2c0abaa 100644 (file)
@@ -138,7 +138,11 @@ EOM
                PublicInbox::MboxReader->$m($fh, sub {
                        push @x, $_[0]->as_string
                });
-               is_deeply(\@x, [], "messages in invalid $m");
+               if ($m =~ /\Amboxcl/) {
+                       is_deeply(\@x, [], "messages in invalid $m");
+               } else {
+                       is_deeply(\@x, [ "\n$html" ], "body-only $m");
+               }
                is_deeply([grep(!/^W: leftover/, @w)], [],
                        "no extra warnings besides leftover ($m)");
        }