]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-edit
No ext_urls
[public-inbox.git] / script / public-inbox-edit
index c98840531fd674c1716aeb0a1aed0b77746d725f..1fbaf5a7d11c19e5759b8a59073de2aa7772876f 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/perl -w
-# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2021 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.
@@ -8,26 +8,41 @@ use strict;
 use warnings;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
 use PublicInbox::AdminEdit;
-use File::Temp qw(tempfile);
-use PublicInbox::ContentId qw(content_id);
+use File::Temp 0.19 (); # 0.19 for TMPDIR
+use PublicInbox::ContentHash qw(content_hash);
 use PublicInbox::MID qw(mid_clean mids);
 PublicInbox::Admin::check_require('-index');
-require PublicInbox::MIME;
-require PublicInbox::InboxWritable;
-require PublicInbox::Import;
+use PublicInbox::Eml;
+use PublicInbox::InboxWritable qw(eml_from_path);
+use PublicInbox::Import;
+
+my $help = <<'EOF';
+usage: public-inbox-edit -m MESSAGE-ID [--all] [INBOX_DIRS]
+
+  destructively edit messages in a public inbox
+
+options:
+
+  --all               edit all configured inboxes
+  -m MESSAGE-ID       edit the message with a given Message-ID
+  -F FILE             edit the message matching the contents of FILE
+  --force             forcibly edit even if Message-ID is ambiguous
+  --raw               do not perform "From " line escaping
+
+See public-inbox-edit(1) man page for full documentation.
+EOF
 
-my $usage = "$0 -m MESSAGE_ID [--all] [INBOX_DIRS]";
 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2, raw => 0 };
-my @opt = qw(mid|m=s file|F=s raw);
-GetOptions($opt, @PublicInbox::AdminEdit::OPT, @opt) or
-       die "bad command-line args\n$usage\n";
+my @opt = qw(mid|m=s file|F=s raw C=s@);
+GetOptions($opt, @PublicInbox::AdminEdit::OPT, @opt) or die $help;
+if ($opt->{help}) { print $help; exit 0 };
+PublicInbox::Admin::do_chdir(delete $opt->{C});
 
+my $cfg = PublicInbox::Config->new;
 my $editor = $ENV{MAIL_EDITOR}; # e.g. "mutt -f"
 unless (defined $editor) {
        my $k = 'publicinbox.mailEditor';
-       if (my $cfg = PublicInbox::Admin::config()) {
-               $editor = $cfg->{lc($k)};
-       }
+       $editor = $cfg->{lc($k)} if $cfg;
        unless (defined $editor) {
                warn "\`$k' not configured, trying \`git var GIT_EDITOR'\n";
                chomp($editor = `git var GIT_EDITOR`);
@@ -41,10 +56,10 @@ if (defined $mid && defined $file) {
        die "the --mid and --file options are mutually exclusive\n";
 }
 
-my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
+my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
 PublicInbox::AdminEdit::check_editable(\@ibxs);
 
-my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
+my $found = {}; # chash => [ [ibx, smsg] [, [ibx, smsg] ] ]
 
 sub find_mid ($$$) {
        my ($found, $mid, $ibxs) = @_;
@@ -53,12 +68,12 @@ sub find_mid ($$$) {
                my ($id, $prev);
                while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
                        my $ref = $ibx->msg_by_smsg($smsg);
-                       my $mime = PublicInbox::MIME->new($ref);
-                       my $cid = content_id($mime);
+                       my $mime = PublicInbox::Eml->new($ref);
+                       my $chash = content_hash($mime);
                        my $tuple = [ $ibx, $smsg ];
-                       push @{$found->{$cid} ||= []}, $tuple
+                       push @{$found->{$chash} ||= []}, $tuple
                }
-               delete @$ibx{qw(over mm git search)}; # cleanup
+               PublicInbox::InboxWritable::cleanup($ibx);
        }
        $found;
 }
@@ -93,13 +108,11 @@ Multiple messages with different content found matching
                warn "Will edit all of them\n";
        }
 } else {
-       open my $fh, '<', $file or die "open($file) failed: $!";
-       my $orig = do { local $/; <$fh> };
-       my $mime = PublicInbox::MIME->new(\$orig);
-       my $mids = mids($mime->header_obj);
+       my $eml = eml_from_path($file) or die "open($file) failed: $!";
+       my $mids = mids($eml);
        find_mid($found, $_, \@ibxs) for (@$mids); # populates $found
-       my $cid = content_id($mime);
-       my $to_edit = $found->{$cid};
+       my $chash = content_hash($eml);
+       my $to_edit = $found->{$chash};
        unless ($to_edit) {
                my $nr = scalar(keys %$found);
                if ($nr > 0) {
@@ -117,16 +130,22 @@ $mids
                }
                exit 1;
        }
-       $found = { $cid => $to_edit };
+       $found = { $chash => $to_edit };
 }
 
-my $tmpl = 'public-inbox-edit-XXXXXX';
+my %tmpopt = (
+       TEMPLATE => 'public-inbox-edit-XXXX',
+       TMPDIR => 1,
+       SUFFIX => $opt->{raw} ? '.eml' : '.mbox',
+);
+
 foreach my $to_edit (values %$found) {
-       my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1, UNLINK => 1);
+       my $edit_fh = File::Temp->new(%tmpopt);
        $edit_fh->autoflush(1);
+       my $edit_fn = $edit_fh->filename;
        my ($ibx, $smsg) = @{$to_edit->[0]};
        my $old_raw = $ibx->msg_by_smsg($smsg);
-       delete @$ibx{qw(over mm git search)}; # cleanup
+       PublicInbox::InboxWritable::cleanup($ibx);
 
        my $tmp = $$old_raw;
        if (!$opt->{raw}) {
@@ -165,7 +184,8 @@ retry_edit:
        # rename/relink $edit_fn
        open my $new_fh, '<', $edit_fn or
                die "can't read edited file ($edit_fn): $!\n";
-       my $new_raw = do { local $/; <$new_fh> };
+       defined(my $new_raw = do { local $/; <$new_fh> }) or die
+               "read $edit_fn: $!\n";
 
        if (!$opt->{raw}) {
                # get rid of the From we added
@@ -201,8 +221,8 @@ W: possible message boundary splitting error
                $new_raw =~ s/^>(>*From )/$1/gm;
        }
 
-       my $new_mime = PublicInbox::MIME->new(\$new_raw);
-       my $old_mime = PublicInbox::MIME->new($old_raw);
+       my $new_mime = PublicInbox::Eml->new(\$new_raw);
+       my $old_mime = PublicInbox::Eml->new($old_raw);
 
        # make sure we don't compare unwanted headers, since mutt adds
        # Content-Length, Status, and Lines headers:
@@ -211,10 +231,10 @@ W: possible message boundary splitting error
 
        # allow changing Received: and maybe other headers which can
        # contain sensitive info.
-       my $nhdr = $new_mime->header_obj;
-       my $ohdr = $old_mime->header_obj;
-       if (($nhdr->as_string eq $ohdr->as_string) &&
-           (content_id($new_mime) eq content_id($old_mime))) {
+       my $nhdr = $new_mime->header_obj->as_string;
+       my $ohdr = $old_mime->header_obj->as_string;
+       if (($nhdr eq $ohdr) &&
+           (content_hash($new_mime) eq content_hash($old_mime))) {
                warn "No change detected to:\n", show_cmd($ibx, $smsg);
 
                next unless $opt->{verbose};