]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
treewide: consolidate "From " line removal
authorEric Wong <e@80x24.org>
Wed, 11 Oct 2023 07:20:53 +0000 (07:20 +0000)
committerEric Wong <e@80x24.org>
Wed, 11 Oct 2023 22:10:48 +0000 (22:10 +0000)
Aside from our prior import bugs (fixed in a0c07cba0e5d8b6a
(mda: drop leading "From " lines again, 2016-06-26)), we'll
always have to be dealing with mutt piping messages to us and
`git format-patch' output.  So just share the regexp so we
can use it everywhere.

In may be desirable to allow importing messages with a leading
"From " line for FUSE, even.

Additionally, some instances of this regexp needlessly added
optional `\r?' (CR) checks ahead of the `\n' (LF) element; but
they're pointless anyways since [^\n]* is enough to exclude all
non-LF bytes.

13 files changed:
lib/PublicInbox/Eml.pm
lib/PublicInbox/IMAP.pm
lib/PublicInbox/Import.pm
lib/PublicInbox/LeiInput.pm
lib/PublicInbox/LeiInspect.pm
lib/PublicInbox/LeiToMail.pm
lib/PublicInbox/Mbox.pm
lib/PublicInbox/MboxReader.pm
lib/PublicInbox/NNTP.pm
script/public-inbox-edit
script/public-inbox-learn
script/public-inbox-mda
script/public-inbox-purge

index 8b999e1a88ef8d11b78e7c5bd0c022dbd7d0f7be..24060ec87ef41c877001fb41145e337c7154b7a8 100644 (file)
@@ -528,4 +528,10 @@ sub willneed { re_memo($_) for @_ }
 willneed(qw(From To Cc Date Subject Content-Type In-Reply-To References
                Message-ID X-Alt-Message-ID));
 
+# This fixes an old bug from import (pre-a0c07cba0e5d8b6a)
+# mutt also pipes single RFC822 messages with a "From " line,
+# but no Content-Length or "From " escaping.
+# "git format-patch" also generates such files by default.
+sub strip_from { $_[0] =~ s/\A[\r\n]*From [^\n]*\n//s }
+
 1;
index 3c64cefa4253d03491461e28eff30e49efbc9919..e4a9e3041806d0615bdef80f2c3e24b79b61b009 100644 (file)
@@ -664,7 +664,7 @@ sub op_eml_new { $_[4] = PublicInbox::Eml->new($_[3]) }
 # s/From / fixes old bug from import (pre-a0c07cba0e5d8b6a)
 sub to_crlf_full {
        ${$_[0]} =~ s/(?<!\r)\n/\r\n/sg;
-       ${$_[0]} =~ s/\A[\r\n]*From [^\r\n]*\r\n//s;
+       PublicInbox::Eml::strip_from(${$_[0]});
 }
 
 sub op_crlf_bref { to_crlf_full($_[3]) }
index 7175884c3b48529e445861ae31de4ffc195a0141..cd03da05d7fe80278844b2747d8e2cbc9f535a4f 100644 (file)
@@ -118,9 +118,6 @@ sub _cat_blob ($$$) {
        $n == $len or croak "cat-blob: short read: $n < $len";
        my $lf = chop $buf;
        croak "bad read on final byte: <$lf>" if $lf ne "\n";
-
-       # fixup some bugginess in old versions:
-       $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
        \$buf;
 }
 
@@ -136,8 +133,9 @@ sub check_remove_v1 {
        my $info = _check_path($r, $w, $tip, $path) or return ('MISSING',undef);
        $info =~ m!\A100644 blob ([a-f0-9]{40,})\t!s or die "not blob: $info";
        my $oid = $1;
-       my $msg = _cat_blob($r, $w, $oid) or die "BUG: cat-blob $1 failed";
-       my $cur = PublicInbox::Eml->new($msg);
+       my $bref = _cat_blob($r, $w, $oid) or die "BUG: cat-blob $1 failed";
+       PublicInbox::Eml::strip_from($$bref);
+       my $cur = PublicInbox::Eml->new($bref);
        my $cur_s = $cur->header('Subject') // '';
        my $cur_m = $mime->header('Subject') // '';
        if ($cur_s ne $cur_m || norm_body($cur) ne norm_body($mime)) {
index 93f8b6b84f047cca9c7732b8baba7dd307a8e88d..28b73ca9e923bb3d9b518ef8f64cdf62d15ec861 100644 (file)
@@ -84,10 +84,7 @@ sub input_fh {
                        return $self->{lei}->child_error(0, <<"");
 error reading $name: $!
 
-               # mutt pipes single RFC822 messages with a "From " line,
-               # but no Content-Length or "From " escaping.
-               # "git format-patch" also generates such files by default.
-               $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+               PublicInbox::Eml::strip_from($buf);
 
                # a user may feed just a body: git diff | lei rediff -U9
                if ($self->{-force_eml}) {
index f801610f7209717023293a818adfba48c2567337..65c64cf29f6c1a4d0021517430dfe5f233b27c68 100644 (file)
@@ -254,7 +254,7 @@ sub inspect_start ($$) {
 sub do_inspect { # lei->do_env cb
        my ($lei) = @_;
        my $str = delete $lei->{istr};
-       $str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       PublicInbox::Eml::strip_from($str);
        my $eml = PublicInbox::Eml->new(\$str);
        inspect_start($lei, [ 'blob:'.$lei->git_oid($eml)->hexdigest,
                        map { "mid:$_" } @{mids($eml)} ]);
index 8771592d36a106b1a7fbce35b4be27a7fa9841bc..ead60b3833043d32c64b068ab062919eb81b8e69 100644 (file)
@@ -53,8 +53,7 @@ sub _mbox_hdr_buf ($$$) {
        }
        my $buf = delete $eml->{hdr};
 
-       # fixup old bug from import (pre-a0c07cba0e5d8b6a)
-       $$buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       PublicInbox::Eml::strip_from($$buf);
        my $ident = $smsg->{blob} // 'lei';
        if (defined(my $pct = $smsg->{pct})) { $ident .= "=$pct" }
 
index bf61bb0e0a56faca1383f5d30e1cf89c41d84b29..52f88ae3c5e665cc899c82b2c1cd19dc63824766 100644 (file)
@@ -89,17 +89,15 @@ sub emit_raw {
 
 sub msg_hdr ($$) {
        my ($ctx, $eml) = @_;
-       my $header_obj = $eml->header_obj;
 
-       # drop potentially confusing headers, ssoma already should've dropped
-       # Lines and Content-Length
-       foreach my $d (qw(Lines Bytes Content-Length Status)) {
-               $header_obj->header_set($d);
+       # drop potentially confusing headers, various importers should've
+       # already dropped these, but we can't trust stuff we've cloned
+       for my $d (qw(Lines Bytes Content-Length Status)) {
+               $eml->header_set($d);
        }
-       my $crlf = $header_obj->crlf;
-       my $buf = $header_obj->as_string;
-       # fixup old bug from import (pre-a0c07cba0e5d8b6a)
-       $buf =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       my $crlf = $eml->crlf;
+       my $buf = $eml->header_obj->as_string;
+       PublicInbox::Eml::strip_from($buf);
        "From mboxrd\@z Thu Jan  1 00:00:00 1970" . $crlf . $buf . $crlf;
 }
 
index e420902239677b29c740c6d677c6248027c29a87..d67fb4ebed06a342d6dda39e3b24e769fc4d4b10 100644 (file)
@@ -93,7 +93,7 @@ sub _mbox_cl ($$$;@) {
                        undef $mbfh;
                }
                while (my $hdr = _extract_hdr(\$buf)) {
-                       $$hdr =~ s/\A[\r\n]*From [^\n]*\n//s or
+                       PublicInbox::Eml::strip_from($$hdr) or
                                die "E: no 'From ' line in:\n", Dumper($hdr);
                        my $eml = PublicInbox::Eml->new($hdr);
                        next unless $eml->raw_size;
index 316b7775865ba1f856c86f6a05a0296e9305a38b..603cf094bf6e96149fcc9909f055099ec809f4d1 100644 (file)
@@ -523,8 +523,7 @@ sub msg_hdr_write ($$) {
        set_nntp_headers($eml, $smsg);
 
        my $hdr = $eml->{hdr} // \(my $x = '');
-       # fixup old bug from import (pre-a0c07cba0e5d8b6a)
-       $$hdr =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       PublicInbox::Eml::strip_from($$hdr);
        $$hdr =~ s/(?<!\r)\n/\r\n/sg; # Alpine barfs without this
 
        # for leafnode compatibility, we need to ensure Message-ID headers
index 1fbaf5a7d11c19e5759b8a59073de2aa7772876f..1fb6f32b45eec240b81fa2463fc9eea939974ac9 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Used for editing messages in a public-inbox.
@@ -188,8 +188,7 @@ retry_edit:
                "read $edit_fn: $!\n";
 
        if (!$opt->{raw}) {
-               # get rid of the From we added
-               $new_raw =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+               PublicInbox::Eml::strip_from($new_raw);
 
                # check if user forgot to purge (in mutt) after editing
                if ($new_raw =~ /^From /sm) {
index 8b8e1b7732938bc8369813866fed9dad59ae6de6..6e1978a7d604d59018e9f24d6798c81d9a6e137c 100755 (executable)
@@ -40,7 +40,7 @@ my $pi_cfg = PublicInbox::Config->new;
 my $err;
 my $mime = PublicInbox::Eml->new(do{
        defined(my $data = do { local $/; <STDIN> }) or die "read STDIN: $!\n";
-       $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+       PublicInbox::Eml::strip_from($data);
 
        if ($train ne 'rm') {
                eval {
index ba4989569e25089d2b7efd90bd4863e0dbe1a838..cac819ac125ac60c8c0d81a1bc1d97c801d0900f 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2013-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Mail delivery agent for public-inbox, run from your MTA upon mail delivery
@@ -39,7 +39,7 @@ use PublicInbox::Spamcheck;
 my $emergency = $ENV{PI_EMERGENCY} || "$ENV{HOME}/.public-inbox/emergency/";
 $ems = PublicInbox::Emergency->new($emergency);
 my $str = do { local $/; <STDIN> };
-$str =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+PublicInbox::Eml::strip_from($str);
 $ems->prepare(\$str);
 my $eml = PublicInbox::Eml->new(\$str);
 my $cfg = PublicInbox::Config->new;
index 121027ccbaa7493a06b6a79a91d0fc328ac887dc..8f9b0b162aa8ede829e3a682857b459ad662a2b4 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2019-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Used for purging messages entirely from a public-inbox.  Currently
@@ -34,7 +34,7 @@ my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
 PublicInbox::AdminEdit::check_editable(\@ibxs);
 
 defined(my $data = do { local $/; <STDIN> }) or die "read STDIN: $!\n";
-$data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
+PublicInbox::Eml::strip_from($data);
 my $n_purged = 0;
 
 foreach my $ibx (@ibxs) {