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 purging messages entirely from a public-inbox. Currently
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 PublicInbox::Admin::check_require('-index');
12 require PublicInbox::Filter::Base;
13 require PublicInbox::MIME;
14 require PublicInbox::V2Writable;
16 { no warnings 'once'; *REJECT = *PublicInbox::Filter::Base::REJECT }
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";
23 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
25 foreach my $ibx (@ibxs) {
26 my $lvl = $ibx->{indexlevel};
28 PublicInbox::Admin::indexlevel_ok_or_die($lvl);
32 # Undefined indexlevel, so `full'...
33 # Search::Xapian exists and the DB can be read, at least, fine
34 $ibx->search and next;
36 # it's possible for a Xapian directory to exist, but Search::Xapian
37 # to go missing/broken. Make sure it's purged in that case:
38 $ibx->over or die "no over.sqlite3 in $ibx->{mainrepo}\n";
40 # $ibx->{search} is populated by $ibx->over call
41 my $xdir_ro = $ibx->{search}->xdir(1);
43 foreach my $part (<$xdir_ro/*>) {
44 if (-d $part && $part =~ m!/[0-9]+\z!) {
46 $bytes += -s $_ foreach glob("$part/*");
51 PublicInbox::Admin::require_or_die('-search');
53 # somebody could "rm -r" all the Xapian directories;
54 # let them purge the overview, at least
55 $ibx->{indexlevel} ||= 'basic';
59 my $data = do { local $/; scalar <STDIN> };
60 $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
63 foreach my $ibx (@ibxs) {
64 my $mime = PublicInbox::MIME->new($data);
65 my $v2w = PublicInbox::V2Writable->new($ibx, 0);
67 my $commits = $v2w->purge($mime) || [];
69 if (my $scrub = $ibx->filter($v2w)) {
70 my $scrubbed = $scrub->scrub($mime, 1);
72 if ($scrubbed && $scrubbed != REJECT()) {
73 my $scrub_commits = $v2w->purge($scrubbed);
74 push @$commits, @$scrub_commits if $scrub_commits;
80 if ($opt->{verbose}) { # should we consider this machine-parseable?
81 print "$ibx->{mainrepo}:";
82 if (scalar @$commits) {
83 print join("\n\t", '', @$commits), "\n";
88 $n_purged += scalar @$commits;
92 exit(0) if ($opt->{force} || $n_purged);
94 warn "Not found\n" if $opt->{verbose};