]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_slrnspool
scripts/import_slrnspool: cleanup progress messages
[public-inbox.git] / scripts / import_slrnspool
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Incremental (or one-shot) importer of a slrnpull news spool
6 =begin usage
7         export ORIGINAL_RECIPIENT=address@example.com
8         public-inbox-init $INBOX $GIT_DIR $HTTP_URL $ORIGINAL_RECIPIENT
9         ./import_slrnspool SLRNPULL_ROOT/news/foo/bar
10 =cut
11 use strict;
12 use warnings;
13 use PublicInbox::Config;
14 use PublicInbox::MIME;
15 use PublicInbox::Import;
16 use PublicInbox::Git;
17 sub usage { "Usage:\n".join('',grep(/\t/, `head -n 10 $0`)) }
18 my $exit = 0;
19 my $sighandler = sub { $exit = 1 };
20 $SIG{INT} = $sighandler;
21 $SIG{TERM} = $sighandler;
22 my $spool = shift @ARGV or die usage();
23 my $recipient = $ENV{ORIGINAL_RECIPIENT};
24 defined $recipient or die usage();
25 my $config = PublicInbox::Config->new;
26 my $ibx = $config->lookup($recipient);
27 my $git = $ibx->git;
28 my $im;
29 if (($ibx->{version} || 1) == 2) {
30         require PublicInbox::V2Writable;
31         $im = PublicInbox::V2Writable->new($ibx);
32         $im->{parallel} = 0; # pointless to be parallel for a single message
33 } else {
34         $im = PublicInbox::Import->new($git, $ibx->{name},
35                                         $ibx->{-primary_address});
36 }
37
38 sub key {
39         "publicinbox.$ibx->{name}.importslrnspoolstate";
40 }
41
42 sub get_min {
43         my $f = PublicInbox::Config->default_file;
44         my $out = $git->qx('config', "--file=$f", key($ibx));
45         $out ||= 0;
46         chomp $out;
47         $out =~ /\A\d+\z/ and return $out;
48         0;
49 }
50
51 sub set_min {
52         my ($num) = @_;
53         my $f = PublicInbox::Config->default_file;
54         my @cmd = (qw/git config/, "--file=$f", key($ibx), $num);
55         system(@cmd) == 0 or die join(' ', @cmd). " failed: $?\n";
56 }
57
58 my $n = get_min();
59 my $ok;
60 my $max_gap = 200000;
61 my $max = $n + $max_gap;
62 $spool =~ s!/+\z!!;
63
64 for (; $exit == 0 && $n < $max; $n++) {
65         my $fn = "$spool/$n";
66         open(my $fh, '<', $fn) or next;
67         $max = $n + $max_gap;
68         print STDERR $fn, "\n";
69
70         my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> });
71         my $hdr = $mime->header_obj;
72
73         # gmane rewrites Received headers, which increases spamminess
74         # Some older archives set Original-To
75         foreach my $x (qw(Received To)) {
76                 my @h = $hdr->header_raw("Original-$x");
77                 if (@h) {
78                         $hdr->header_set($x, @h);
79                         $hdr->header_set("Original-$x");
80                 }
81         }
82
83         # Approved triggers for the SA HEADER_SPAM rule,
84         # X-From is gmane specific
85         foreach my $drop (qw(Approved X-From)) {
86                 $hdr->header_set($drop);
87         }
88
89         # appears to be an old gmane bug:
90         $hdr->header_set('connect()');
91         $im->add($mime);
92
93         $ok = $n + 1;
94         set_min($ok);
95 }
96
97 $im->done;