]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-purge
Merge remote-tracking branch 'origin/reshard' into next
[public-inbox.git] / script / public-inbox-purge
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 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 require PublicInbox::Filter::Base;
13 require PublicInbox::MIME;
14 require PublicInbox::V2Writable;
15
16 { no warnings 'once'; *REJECT = *PublicInbox::Filter::Base::REJECT }
17
18 my $usage = "$0 [--all] [INBOX_DIRS] </path/to/message";
19 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2 };
20 GetOptions($opt, @PublicInbox::AdminEdit::OPT) or
21         die "bad command-line args\n$usage\n";
22
23 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
24 PublicInbox::AdminEdit::check_editable(\@ibxs);
25
26 my $data = do { local $/; scalar <STDIN> };
27 $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
28 my $n_purged = 0;
29
30 foreach my $ibx (@ibxs) {
31         my $mime = PublicInbox::MIME->new($data);
32         my $v2w = PublicInbox::V2Writable->new($ibx, 0);
33
34         my $commits = $v2w->purge($mime) || [];
35
36         if (my $scrub = $ibx->filter($v2w)) {
37                 my $scrubbed = $scrub->scrub($mime, 1);
38
39                 if ($scrubbed && $scrubbed != REJECT()) {
40                         my $scrub_commits = $v2w->purge($scrubbed);
41                         push @$commits, @$scrub_commits if $scrub_commits;
42                 }
43         }
44
45         $v2w->done;
46
47         if ($opt->{verbose}) { # should we consider this machine-parseable?
48                 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, $commits);
49         }
50         $n_purged += scalar @$commits;
51 }
52
53 # behave like "rm -f"
54 exit(0) if ($opt->{force} || $n_purged);
55
56 warn "Not found\n" if $opt->{verbose};
57 exit(1);