]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-edit
edit|purge: improve output on rewrites
[public-inbox.git] / script / public-inbox-edit
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Used for editing messages in a public-inbox.
6 # Supports v2 inboxes only, for now.
7 use strict;
8 use warnings;
9 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
10 use PublicInbox::AdminEdit;
11 use File::Temp qw(tempfile);
12 use PublicInbox::ContentId qw(content_id);
13 use PublicInbox::MID qw(mid_clean mids);
14 PublicInbox::Admin::check_require('-index');
15 require PublicInbox::MIME;
16 require PublicInbox::InboxWritable;
17
18 my $usage = "$0 -m MESSAGE_ID [--all] [INBOX_DIRS]";
19 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2, raw => 0 };
20 my @opt = qw(mid|m=s file|F=s raw);
21 GetOptions($opt, @PublicInbox::AdminEdit::OPT, @opt) or
22         die "bad command-line args\n$usage\n";
23
24 my $editor = $ENV{MAIL_EDITOR}; # e.g. "mutt -f"
25 unless (defined $editor) {
26         my $k = 'publicinbox.mailEditor';
27         if (my $cfg = PublicInbox::Admin::config()) {
28                 $editor = $cfg->{lc($k)};
29         }
30         unless (defined $editor) {
31                 warn "\`$k' not configured, trying \`git var GIT_EDITOR'\n";
32                 chomp($editor = `git var GIT_EDITOR`);
33                 warn "Will use $editor to edit mail\n";
34         }
35 }
36
37 my $mid = $opt->{mid};
38 my $file = $opt->{file};
39 if (defined $mid && defined $file) {
40         die "the --mid and --file options are mutually exclusive\n";
41 }
42
43 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
44 PublicInbox::AdminEdit::check_editable(\@ibxs);
45
46 my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
47
48 sub find_mid ($) {
49         my ($mid) = @_;
50         foreach my $ibx (@ibxs) {
51                 my $over = $ibx->over;
52                 my ($id, $prev);
53                 while (my $smsg = $over->next_by_mid($mid, \$id, \$prev)) {
54                         my $ref = $ibx->msg_by_smsg($smsg);
55                         my $mime = PublicInbox::MIME->new($ref);
56                         my $cid = content_id($mime);
57                         my $tuple = [ $ibx, $smsg ];
58                         push @{$found->{$cid} ||= []}, $tuple
59                 }
60                 delete @$ibx{qw(over mm git search)}; # cleanup
61         }
62         $found;
63 }
64
65 sub show_cmd ($$) {
66         my ($ibx, $smsg) = @_;
67         " GIT_DIR=$ibx->{mainrepo}/all.git \\\n    git show $smsg->{blob}\n";
68 }
69
70 sub show_found () {
71         foreach my $to_edit (values %$found) {
72                 foreach my $tuple (@$to_edit) {
73                         my ($ibx, $smsg) = @$tuple;
74                         warn show_cmd($ibx, $smsg);
75                 }
76         }
77 }
78
79 if (defined($mid)) {
80         $mid = mid_clean($mid);
81         $found = find_mid($mid);
82         my $nr = scalar(keys %$found);
83         die "No message found for <$mid>\n" unless $nr;
84         if ($nr > 1) {
85                 warn <<"";
86 Multiple messages with different content found matching
87 <$mid>:
88
89                 show_found();
90                 die "Use --force to edit all of them\n" if !$opt->{force};
91                 warn "Will edit all of them\n";
92         }
93 } else {
94         open my $fh, '<', $file or die "open($file) failed: $!";
95         my $orig = do { local $/; <$fh> };
96         my $mime = PublicInbox::MIME->new(\$orig);
97         my $mids = mids($mime->header_obj);
98         find_mid($_) for (@$mids); # populates $found
99         my $cid = content_id($mime);
100         my $to_edit = $found->{$cid};
101         unless ($to_edit) {
102                 my $nr = scalar(keys %$found);
103                 if ($nr > 0) {
104                         warn <<"";
105 $nr matches to Message-ID(s) in $file, but none matched content
106 Partial matches below:
107
108                         show_found();
109                 } elsif ($nr == 0) {
110                         $mids = join('', map { "  <$_>\n" } @$mids);
111                         warn <<"";
112 No matching messages found matching Message-ID(s) in $file
113 $mids
114
115                 }
116                 exit 1;
117         }
118         $found = { $cid => $to_edit };
119 }
120
121 my $tmpl = 'public-inbox-edit-XXXXXX';
122 foreach my $to_edit (values %$found) {
123         my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1);
124         $edit_fh->autoflush(1);
125         my ($ibx, $smsg) = @{$to_edit->[0]};
126         my $old_raw = $ibx->msg_by_smsg($smsg);
127         delete @$ibx{qw(over mm git search)}; # cleanup
128
129         my $tmp = $$old_raw;
130         if (!$opt->{raw}) {
131                 my $oid = $smsg->{blob};
132                 print $edit_fh "From mboxrd\@$oid Thu Jan  1 00:00:00 1970\n";
133                 $tmp =~ s/^(>*From )/>$1/gm;
134         }
135         print $edit_fh $tmp or
136                 die "failed to write tempfile for editing: $!";
137
138         # run the editor, respecting spaces/quote
139 retry_edit:
140         if (system(qw(sh -c), qq(eval "$editor" '"\$@"'), '--', $edit_fn)) {
141                 if (!(-t STDIN) && !$opt->{force}) {
142                         die "E: $editor failed: $?\n";
143                 }
144                 print STDERR "$editor failed, ";
145                 print STDERR "continuing as forced\n" if $opt->{force};
146                 while (!$opt->{force}) {
147                         print STDERR "(r)etry, (c)ontinue, (q)uit?\n";
148                         chomp(my $op = <STDIN> || '');
149                         $op = lc($op);
150                         goto retry_edit if $op eq 'r';
151                         exit $? if $op eq 'q';
152                         last if $op eq 'c'; # continuing
153                         print STDERR "\`$op' not recognized\n";
154                 }
155         }
156
157         # reread the edited file, not using $edit_fh since $EDITOR may
158         # rename/relink $edit_fn
159         open my $new_fh, '<', $edit_fn or
160                 die "can't read edited file ($edit_fn): $!\n";
161         my $new_raw = do { local $/; <$new_fh> };
162
163         if (!$opt->{raw}) {
164                 # get rid of the From we added
165                 $new_raw =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
166
167                 # check if user forgot to purge (in mutt) after editing
168                 if ($new_raw =~ /^From /sm) {
169                         if (-t STDIN) {
170                                 print STDERR <<'';
171 Extra "From " lines detected in new mbox.
172 Did you forget to purge the original message from the mbox after editing?
173
174                                 while (1) {
175                                         print STDERR <<"";
176 (y)es to re-edit, (n)o to continue
177
178                                         chomp(my $op = <STDIN> || '');
179                                         $op = lc($op);
180                                         goto retry_edit if $op eq 'y';
181                                         last if $op eq 'n'; # continuing
182                                         print STDERR "\`$op' not recognized\n";
183                                 }
184                         } else { # non-interactive path
185                                 # unlikely to happen, as extra From lines are
186                                 # only a common mistake (for me) with
187                                 # interactive use
188                                 warn <<"";
189 W: possible message boundary splitting error
190
191                         }
192                 }
193                 # unescape what we escaped:
194                 $new_raw =~ s/^>(>*From )/$1/gm;
195         }
196
197         my $new_mime = PublicInbox::MIME->new(\$new_raw);
198         my $old_mime = PublicInbox::MIME->new($old_raw);
199
200         # allow changing Received: and maybe other headers which can
201         # contain sensitive info.
202         my $nhdr = $new_mime->header_obj;
203         my $ohdr = $old_mime->header_obj;
204         if (($nhdr->as_string eq $ohdr->as_string) &&
205             (content_id($new_mime) eq content_id($old_mime))) {
206                 warn "No change detected to:\n", show_cmd($ibx, $smsg);
207
208                 next unless $opt->{verbose};
209                 # should we consider this machine-parseable?
210                 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, []);
211                 next;
212         }
213
214         foreach my $tuple (@$to_edit) {
215                 $ibx = PublicInbox::InboxWritable->new($tuple->[0]);
216                 $smsg = $tuple->[1];
217                 my $im = $ibx->importer(0);
218                 my $commits = $im->replace($old_mime, $new_mime);
219                 $im->done;
220                 unless ($commits) {
221                         warn "Failed to replace:\n", show_cmd($ibx, $smsg);
222                         next;
223                 }
224                 next unless $opt->{verbose};
225                 # should we consider this machine-parseable?
226                 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, $commits);
227         }
228 }