]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_slrnspool
inbox: add ->version method
[public-inbox.git] / scripts / import_slrnspool
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015-2019 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 == 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 $ibx->{filter} ||= 'PublicInbox::Filter::Gmane';
39 my $filter = $ibx->filter;
40
41 sub key {
42         "publicinbox.$ibx->{name}.importslrnspoolstate";
43 }
44
45 sub get_min {
46         my $f = PublicInbox::Config->default_file;
47         my $out = $git->qx('config', "--file=$f", key($ibx));
48         $out ||= 0;
49         chomp $out;
50         $out =~ /\A[0-9]+\z/ and return $out;
51         0;
52 }
53
54 sub set_min {
55         my ($num) = @_;
56         my $f = PublicInbox::Config->default_file;
57         my @cmd = (qw/git config/, "--file=$f", key($ibx), $num);
58         system(@cmd) == 0 or die join(' ', @cmd). " failed: $?\n";
59 }
60
61 my $n = get_min();
62 my $ok;
63 my $max_gap = 200000;
64 my $max = $n + $max_gap;
65 $spool =~ s!/+\z!!;
66
67 for (; $exit == 0 && $n < $max; $n++) {
68         my $fn = "$spool/$n";
69         open(my $fh, '<', $fn) or next;
70         $max = $n + $max_gap;
71         print STDERR $fn, "\n";
72
73         my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> });
74         $filter->scrub($mime);
75         $im->add($mime);
76
77         $ok = $n + 1;
78         set_min($ok);
79 }
80
81 $im->done;