]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-purge
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / script / public-inbox-purge
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Used for purging messages entirely from a public-inbox.  Currently
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 PublicInbox::Admin::check_require('-index');
12 use PublicInbox::Filter::Base qw(REJECT);
13 use PublicInbox::MIME;
14 require PublicInbox::V2Writable;
15
16 my $usage = "$0 [--all] [INBOX_DIRS] </path/to/message";
17 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2 };
18 GetOptions($opt, @PublicInbox::AdminEdit::OPT) or
19         die "bad command-line args\n$usage\n";
20
21 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
22 PublicInbox::AdminEdit::check_editable(\@ibxs);
23
24 my $data = do { local $/; scalar <STDIN> };
25 $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
26 my $n_purged = 0;
27
28 foreach my $ibx (@ibxs) {
29         my $mime = PublicInbox::MIME->new($data);
30         my $v2w = PublicInbox::V2Writable->new($ibx, 0);
31
32         my $commits = $v2w->purge($mime) || [];
33
34         if (my $scrub = $ibx->filter($v2w)) {
35                 my $scrubbed = $scrub->scrub($mime, 1);
36
37                 if ($scrubbed && $scrubbed != REJECT()) {
38                         my $scrub_commits = $v2w->purge($scrubbed);
39                         push @$commits, @$scrub_commits if $scrub_commits;
40                 }
41         }
42
43         $v2w->done;
44
45         if ($opt->{verbose}) { # should we consider this machine-parseable?
46                 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, $commits);
47         }
48         $n_purged += scalar @$commits;
49 }
50
51 # behave like "rm -f"
52 exit(0) if ($opt->{force} || $n_purged);
53
54 warn "Not found\n" if $opt->{verbose};
55 exit(1);