]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_slrnspool
import_slrnspool: reimplement using fast-import
[public-inbox.git] / scripts / import_slrnspool
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015-2016 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 Email::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 = PublicInbox::Import->new($git, $ibx->{name}, $ibx->{-primary_address});
29
30 sub key {
31         "publicinbox.$ibx->{name}.importslrnspoolstate";
32 }
33
34 sub get_min {
35         my $f = PublicInbox::Config->default_file;
36         my $out = $git->qx('config', "--file=$f", key($ibx));
37         $out ||= 0;
38         chomp $out;
39         $out =~ /\A\d+\z/ and return $out;
40         0;
41 }
42
43 sub set_min {
44         my ($num) = @_;
45         my $f = PublicInbox::Config->default_file;
46         my @cmd = (qw/git config/, "--file=$f", key($ibx), $num);
47         system(@cmd) == 0 or die join(' ', @cmd). " failed: $?\n";
48 }
49
50 my $n = get_min();
51 my $ok;
52 my $max_gap = 200000;
53 my $max = $n + $max_gap;
54
55 for (; $exit == 0 && $n < $max; $n++) {
56         my $fn = "$spool/$n";
57         print STDERR $fn, "\n";
58         open(my $fh, '<', $fn) or next;
59         $max = $n + $max_gap;
60
61         my $mime = Email::MIME->new(eval { local $/; <$fh> });
62         my $hdr = $mime->header_obj;
63
64         # gmane rewrites Received headers, which increases spamminess
65         # Some older archives set Original-To
66         foreach my $x (qw(Received To)) {
67                 my @h = $hdr->header_raw("Original-$x");
68                 if (@h) {
69                         $hdr->header_set($x, @h);
70                         $hdr->header_set("Original-$x");
71                 }
72         }
73
74         # Approved triggers for the SA HEADER_SPAM rule,
75         # X-From is gmane specific
76         foreach my $drop (qw(Approved X-From)) {
77                 $hdr->header_set($drop);
78         }
79
80         # appears to be an old gmane bug:
81         $hdr->header_set('connect()');
82         $im->add($mime);
83
84         $ok = $n + 1;
85         set_min($ok);
86 }
87
88 $im->done;