]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_slrnspool
scripts/import_slrnspool: support v2 repos
[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
63 for (; $exit == 0 && $n < $max; $n++) {
64         my $fn = "$spool/$n";
65         print STDERR $fn, "\n";
66         open(my $fh, '<', $fn) or next;
67         $max = $n + $max_gap;
68
69         my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> });
70         my $hdr = $mime->header_obj;
71
72         # gmane rewrites Received headers, which increases spamminess
73         # Some older archives set Original-To
74         foreach my $x (qw(Received To)) {
75                 my @h = $hdr->header_raw("Original-$x");
76                 if (@h) {
77                         $hdr->header_set($x, @h);
78                         $hdr->header_set("Original-$x");
79                 }
80         }
81
82         # Approved triggers for the SA HEADER_SPAM rule,
83         # X-From is gmane specific
84         foreach my $drop (qw(Approved X-From)) {
85                 $hdr->header_set($drop);
86         }
87
88         # appears to be an old gmane bug:
89         $hdr->header_set('connect()');
90         $im->add($mime);
91
92         $ok = $n + 1;
93         set_min($ok);
94 }
95
96 $im->done;