2 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # Used for editing messages in a public-inbox.
6 # Supports v2 inboxes only, for now.
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;
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";
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)};
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";
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";
43 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
44 PublicInbox::AdminEdit::check_editable(\@ibxs);
46 my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
50 foreach my $ibx (@ibxs) {
51 my $over = $ibx->over;
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
60 delete @$ibx{qw(over mm git search)}; # cleanup
66 my ($ibx, $smsg) = @_;
67 " GIT_DIR=$ibx->{mainrepo}/all.git \\\n git show $smsg->{blob}\n";
71 foreach my $to_edit (values %$found) {
72 foreach my $tuple (@$to_edit) {
73 my ($ibx, $smsg) = @$tuple;
74 warn show_cmd($ibx, $smsg);
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;
86 Multiple messages with different content found matching
90 die "Use --force to edit all of them\n" if !$opt->{force};
91 warn "Will edit all of them\n";
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};
102 my $nr = scalar(keys %$found);
105 $nr matches to Message-ID(s) in $file, but none matched content
106 Partial matches below:
110 $mids = join('', map { " <$_>\n" } @$mids);
112 No matching messages found matching Message-ID(s) in $file
118 $found = { $cid => $to_edit };
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
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;
135 print $edit_fh $tmp or
136 die "failed to write tempfile for editing: $!";
138 # run the editor, respecting spaces/quote
140 if (system(qw(sh -c), qq(eval "$editor" '"\$@"'), '--', $edit_fn)) {
141 if (!(-t STDIN) && !$opt->{force}) {
142 die "E: $editor failed: $?\n";
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> || '');
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";
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> };
164 # get rid of the From we added
165 $new_raw =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
167 # check if user forgot to purge (in mutt) after editing
168 if ($new_raw =~ /^From /sm) {
171 Extra "From " lines detected in new mbox.
172 Did you forget to purge the original message from the mbox after editing?
176 (y)es to re-edit, (n)o to continue
178 chomp(my $op = <STDIN> || '');
180 goto retry_edit if $op eq 'y';
181 last if $op eq 'n'; # continuing
182 print STDERR "\`$op' not recognized\n";
184 } else { # non-interactive path
185 # unlikely to happen, as extra From lines are
186 # only a common mistake (for me) with
189 W: possible message boundary splitting error
193 # unescape what we escaped:
194 $new_raw =~ s/^>(>*From )/$1/gm;
197 my $new_mime = PublicInbox::MIME->new(\$new_raw);
198 my $old_mime = PublicInbox::MIME->new($old_raw);
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);
208 next unless $opt->{verbose};
209 # should we consider this machine-parseable?
210 print "$ibx->{mainrepo}:\n\tNONE\n";
214 foreach my $tuple (@$to_edit) {
215 $ibx = PublicInbox::InboxWritable->new($tuple->[0]);
217 my $im = $ibx->importer(0);
218 my $commits = $im->replace($old_mime, $new_mime);
221 warn "Failed to replace:\n", show_cmd($ibx, $smsg);
224 next unless $opt->{verbose};
225 # should we consider this machine-parseable?
226 print "$ibx->{mainrepo}:";
227 if (scalar @$commits) {
228 print join("\n\t", '', @$commits), "\n";