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