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;
17 require PublicInbox::Import;
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";
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)};
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";
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";
44 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
45 PublicInbox::AdminEdit::check_editable(\@ibxs);
47 my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
51 foreach my $ibx (@ibxs) {
52 my $over = $ibx->over;
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
61 delete @$ibx{qw(over mm git search)}; # cleanup
67 my ($ibx, $smsg) = @_;
68 " GIT_DIR=$ibx->{mainrepo}/all.git \\\n git show $smsg->{blob}\n";
72 foreach my $to_edit (values %$found) {
73 foreach my $tuple (@$to_edit) {
74 my ($ibx, $smsg) = @$tuple;
75 warn show_cmd($ibx, $smsg);
81 $mid = mid_clean($mid);
82 $found = find_mid($mid);
83 my $nr = scalar(keys %$found);
84 die "No message found for <$mid>\n" unless $nr;
87 Multiple messages with different content found matching
91 die "Use --force to edit all of them\n" if !$opt->{force};
92 warn "Will edit all of them\n";
95 open my $fh, '<', $file or die "open($file) failed: $!";
96 my $orig = do { local $/; <$fh> };
97 my $mime = PublicInbox::MIME->new(\$orig);
98 my $mids = mids($mime->header_obj);
99 find_mid($_) for (@$mids); # populates $found
100 my $cid = content_id($mime);
101 my $to_edit = $found->{$cid};
103 my $nr = scalar(keys %$found);
106 $nr matches to Message-ID(s) in $file, but none matched content
107 Partial matches below:
111 $mids = join('', map { " <$_>\n" } @$mids);
113 No matching messages found matching Message-ID(s) in $file
119 $found = { $cid => $to_edit };
122 my $tmpl = 'public-inbox-edit-XXXXXX';
123 foreach my $to_edit (values %$found) {
124 my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1, UNLINK => 1);
125 $edit_fh->autoflush(1);
126 my ($ibx, $smsg) = @{$to_edit->[0]};
127 my $old_raw = $ibx->msg_by_smsg($smsg);
128 delete @$ibx{qw(over mm git search)}; # cleanup
132 my $oid = $smsg->{blob};
133 print $edit_fh "From mboxrd\@$oid Thu Jan 1 00:00:00 1970\n";
134 $tmp =~ s/^(>*From )/>$1/gm;
136 print $edit_fh $tmp or
137 die "failed to write tempfile for editing: $!";
139 # run the editor, respecting spaces/quote
141 if (system(qw(sh -c), $editor.' "$@"', $editor, $edit_fn)) {
142 if (!(-t STDIN) && !$opt->{force}) {
143 die "E: $editor failed: $?\n";
145 print STDERR "$editor failed, ";
146 print STDERR "continuing as forced\n" if $opt->{force};
147 while (!$opt->{force}) {
148 print STDERR "(r)etry, (c)ontinue, (q)uit?\n";
149 chomp(my $op = <STDIN> || '');
151 goto retry_edit if $op eq 'r';
152 exit $? if $op eq 'q';
153 last if $op eq 'c'; # continuing
154 print STDERR "\`$op' not recognized\n";
158 # reread the edited file, not using $edit_fh since $EDITOR may
159 # rename/relink $edit_fn
160 open my $new_fh, '<', $edit_fn or
161 die "can't read edited file ($edit_fn): $!\n";
162 my $new_raw = do { local $/; <$new_fh> };
165 # get rid of the From we added
166 $new_raw =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
168 # check if user forgot to purge (in mutt) after editing
169 if ($new_raw =~ /^From /sm) {
172 Extra "From " lines detected in new mbox.
173 Did you forget to purge the original message from the mbox after editing?
177 (y)es to re-edit, (n)o to continue
179 chomp(my $op = <STDIN> || '');
181 goto retry_edit if $op eq 'y';
182 last if $op eq 'n'; # continuing
183 print STDERR "\`$op' not recognized\n";
185 } else { # non-interactive path
186 # unlikely to happen, as extra From lines are
187 # only a common mistake (for me) with
190 W: possible message boundary splitting error
194 # unescape what we escaped:
195 $new_raw =~ s/^>(>*From )/$1/gm;
198 my $new_mime = PublicInbox::MIME->new(\$new_raw);
199 my $old_mime = PublicInbox::MIME->new($old_raw);
201 # make sure we don't compare unwanted headers, since mutt adds
202 # Content-Length, Status, and Lines headers:
203 PublicInbox::Import::drop_unwanted_headers($new_mime);
204 PublicInbox::Import::drop_unwanted_headers($old_mime);
206 # allow changing Received: and maybe other headers which can
207 # contain sensitive info.
208 my $nhdr = $new_mime->header_obj;
209 my $ohdr = $old_mime->header_obj;
210 if (($nhdr->as_string eq $ohdr->as_string) &&
211 (content_id($new_mime) eq content_id($old_mime))) {
212 warn "No change detected to:\n", show_cmd($ibx, $smsg);
214 next unless $opt->{verbose};
215 # should we consider this machine-parseable?
216 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, []);
220 foreach my $tuple (@$to_edit) {
221 $ibx = PublicInbox::InboxWritable->new($tuple->[0]);
223 my $im = $ibx->importer(0);
224 my $commits = $im->replace($old_mime, $new_mime);
227 warn "Failed to replace:\n", show_cmd($ibx, $smsg);
230 next unless $opt->{verbose};
231 # should we consider this machine-parseable?
232 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, $commits);