X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=script%2Fpublic-inbox-edit;h=e408c7bccdd260f1e37086d41e095240e873552d;hb=55b707d788ce13696e4411389583e720ea6dab01;hp=7a534cc2b094bc5f796a34aec32ce76dbda0a48a;hpb=6e507c8cb41b0d48963503a88034348d74506211;p=public-inbox.git diff --git a/script/public-inbox-edit b/script/public-inbox-edit index 7a534cc2..e408c7bc 100755 --- a/script/public-inbox-edit +++ b/script/public-inbox-edit @@ -8,12 +8,13 @@ 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 File::Temp 0.19 (); # 0.19 for TMPDIR use PublicInbox::ContentId qw(content_id); use PublicInbox::MID qw(mid_clean mids); PublicInbox::Admin::check_require('-index'); -require PublicInbox::MIME; -require PublicInbox::InboxWritable; +use PublicInbox::MIME; +use PublicInbox::InboxWritable; +use PublicInbox::Import; my $usage = "$0 -m MESSAGE_ID [--all] [INBOX_DIRS]"; my $opt = { verbose => 1, all => 0, -min_inbox_version => 2, raw => 0 }; @@ -21,12 +22,11 @@ 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 $cfg = eval { 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`); @@ -40,14 +40,14 @@ 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] ] ] -sub find_mid ($) { - my ($mid) = @_; - foreach my $ibx (@ibxs) { +sub find_mid ($$$) { + my ($found, $mid, $ibxs) = @_; + foreach my $ibx (@$ibxs) { my $over = $ibx->over; my ($id, $prev); while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) { @@ -57,17 +57,18 @@ sub find_mid ($) { my $tuple = [ $ibx, $smsg ]; push @{$found->{$cid} ||= []}, $tuple } - delete @$ibx{qw(over mm git search)}; # cleanup + PublicInbox::InboxWritable::cleanup($ibx); } $found; } sub show_cmd ($$) { my ($ibx, $smsg) = @_; - " GIT_DIR=$ibx->{mainrepo}/all.git \\\n git show $smsg->{blob}\n"; + " GIT_DIR=$ibx->{inboxdir}/all.git \\\n git show $smsg->{blob}\n"; } -sub show_found () { +sub show_found ($) { + my ($found) = @_; foreach my $to_edit (values %$found) { foreach my $tuple (@$to_edit) { my ($ibx, $smsg) = @$tuple; @@ -78,7 +79,7 @@ sub show_found () { if (defined($mid)) { $mid = mid_clean($mid); - $found = find_mid($mid); + find_mid($found, $mid, \@ibxs); my $nr = scalar(keys %$found); die "No message found for <$mid>\n" unless $nr; if ($nr > 1) { @@ -86,7 +87,7 @@ if (defined($mid)) { Multiple messages with different content found matching <$mid>: - show_found(); + show_found($found); die "Use --force to edit all of them\n" if !$opt->{force}; warn "Will edit all of them\n"; } @@ -95,7 +96,7 @@ Multiple messages with different content found matching my $orig = do { local $/; <$fh> }; my $mime = PublicInbox::MIME->new(\$orig); my $mids = mids($mime->header_obj); - find_mid($_) for (@$mids); # populates $found + find_mid($found, $_, \@ibxs) for (@$mids); # populates $found my $cid = content_id($mime); my $to_edit = $found->{$cid}; unless ($to_edit) { @@ -105,7 +106,7 @@ Multiple messages with different content found matching $nr matches to Message-ID(s) in $file, but none matched content Partial matches below: - show_found(); + show_found($found); } elsif ($nr == 0) { $mids = join('', map { " <$_>\n" } @$mids); warn <<""; @@ -118,18 +119,25 @@ $mids $found = { $cid => $to_edit }; } -my $tmpl = 'public-inbox-edit-XXXXXX'; +my %tmpopt = ( + TEMPLATE => 'public-inbox-edit-XXXXXX', + TMPDIR => 1, + SUFFIX => $opt->{raw} ? '.eml' : '.mbox', +); + foreach my $to_edit (values %$found) { - my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 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}) { my $oid = $smsg->{blob}; - print $edit_fh "From mboxrd\@$oid Thu Jan 1 00:00:00 1970\n"; + print $edit_fh "From mboxrd\@$oid Thu Jan 1 00:00:00 1970\n" + or die "failed to write From_ line: $!"; $tmp =~ s/^(>*From )/>$1/gm; } print $edit_fh $tmp or @@ -137,7 +145,7 @@ foreach my $to_edit (values %$found) { # run the editor, respecting spaces/quote retry_edit: - if (system(qw(sh -c), qq(eval "$editor" '"\$@"'), '--', $edit_fn)) { + if (system(qw(sh -c), $editor.' "$@"', $editor, $edit_fn)) { if (!(-t STDIN) && !$opt->{force}) { die "E: $editor failed: $?\n"; } @@ -148,7 +156,11 @@ retry_edit: chomp(my $op = || ''); $op = lc($op); goto retry_edit if $op eq 'r'; - exit $? if $op eq 'q'; + if ($op eq 'q') { + # n.b. we'll lose the exit signal, here, + # oh well; "q" is user-specified anyways. + exit($? >> 8); + } last if $op eq 'c'; # continuing print STDERR "\`$op' not recognized\n"; } @@ -197,6 +209,11 @@ W: possible message boundary splitting error my $new_mime = PublicInbox::MIME->new(\$new_raw); my $old_mime = PublicInbox::MIME->new($old_raw); + # make sure we don't compare unwanted headers, since mutt adds + # Content-Length, Status, and Lines headers: + PublicInbox::Import::drop_unwanted_headers($new_mime); + PublicInbox::Import::drop_unwanted_headers($old_mime); + # allow changing Received: and maybe other headers which can # contain sensitive info. my $nhdr = $new_mime->header_obj;