]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-purge
No ext_urls
[public-inbox.git] / script / public-inbox-purge
1 #!/usr/bin/perl -w
2 # Copyright (C) 2019-2021 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::Eml;
14 require PublicInbox::V2Writable;
15
16 my $help = <<EOF;
17 usage: public-inbox-purge [--all] [INBOX_DIRS] </path/to/message
18
19   erase message entirely from an inbox (including history)
20
21 options:
22
23   --all               purge from all configured inboxes
24
25 See public-inbox-purge(1) man page for full documentation.
26 EOF
27
28 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2 };
29 GetOptions($opt, @PublicInbox::AdminEdit::OPT, 'C=s@') or die $help;
30 if ($opt->{help}) { print $help; exit 0 };
31
32 PublicInbox::Admin::do_chdir(delete $opt->{C});
33 my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
34 PublicInbox::AdminEdit::check_editable(\@ibxs);
35
36 defined(my $data = do { local $/; <STDIN> }) or die "read STDIN: $!\n";
37 $data =~ s/\A[\r\n]*From [^\r\n]*\r?\n//s;
38 my $n_purged = 0;
39
40 foreach my $ibx (@ibxs) {
41         my $mime = PublicInbox::Eml->new($data);
42         my $v2w = PublicInbox::V2Writable->new($ibx, 0);
43
44         my $commits = $v2w->purge($mime) || [];
45
46         if (my $scrub = $ibx->filter($v2w)) {
47                 my $scrubbed = $scrub->scrub($mime, 1);
48
49                 if ($scrubbed && $scrubbed != REJECT()) {
50                         my $scrub_commits = $v2w->purge($scrubbed);
51                         push @$commits, @$scrub_commits if $scrub_commits;
52                 }
53         }
54
55         $v2w->done;
56
57         if ($opt->{verbose}) { # should we consider this machine-parseable?
58                 PublicInbox::AdminEdit::show_rewrites(\*STDOUT, $ibx, $commits);
59         }
60         $n_purged += scalar @$commits;
61 }
62
63 # behave like "rm -f"
64 exit(0) if ($opt->{force} || $n_purged);
65
66 warn "Not found\n" if $opt->{verbose};
67 exit(1);