]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_slrnspool
import_slrnspool: graceful exit for interruptibility
[public-inbox.git] / scripts / import_slrnspool
1 #!/usr/bin/perl -w
2 # Copyright (C) 2015, all contributors <meta@public-inbox.org>
3 # License: AGPLv3 or later (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 $LISTNAME $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::Filter;
15 use Email::LocalDelivery;
16 sub usage { "Usage:\n".join('',grep(/\t/, `head -n 10 $0`)) }
17 my $exit = 0;
18 my $sighandler = sub { $exit = 1 };
19 $SIG{INT} = $sighandler;
20 $SIG{TERM} = $sighandler;
21 my $spool = shift @ARGV or die usage();
22 my $recipient = $ENV{ORIGINAL_RECIPIENT};
23 defined $recipient or die usage();
24 my $config = PublicInbox::Config->new;
25 my $cfg = $config->lookup($recipient);
26 defined $cfg or exit(1);
27 my @mda;
28 if ($ENV{'FILTER'}) {
29         @mda = qw(public-inbox-mda);
30 } else {
31         @mda = (qw(ssoma-mda -1), $cfg->{mainrepo});
32 }
33
34 sub get_min {
35         my ($cfg) = @_;
36         $cfg->{importslrnspoolstate} || 0;
37 }
38
39 sub set_min {
40         my ($cfg, $num) = @_;
41         my $f = PublicInbox::Config->default_file;
42         my @cmd = (qw/git config/, "--file=$f",
43                    "publicinbox.$cfg->{listname}.importslrnspoolstate", $num);
44         system(@cmd) == 0 or die join(' ', @cmd). " failed: $?\n";
45 }
46
47 my $n = get_min();
48 my $ok;
49 my $max_gap = 10000;
50 my $max = $n + $max_gap;
51
52 for (; $exit == 0 && $n < $max; $n++) {
53         my $fn = "$spool/$n";
54         print STDERR $fn, "\n";
55         open(my $fh, '<', $fn) or next;
56         $max = $n + $max_gap;
57         my $f = Email::Filter->new(data => eval { local $/; <$fh> });
58         my $s = $f->simple;
59
60         # gmane rewrites Received headers, which increases spamminess
61         # Some older archives set Original-To
62         foreach my $x (qw(Received To)) {
63                 my @h = $s->header("Original-$x");
64                 if (@h) {
65                         $s->header_set($x, @h);
66                         $s->header_set("Original-$x");
67                 }
68         }
69
70         # triggers for the SA HEADER_SPAM rule
71         foreach my $drop (qw(Approved)) { $s->header_set($drop) }
72
73         # appears to be an old gmane bug:
74         $s->header_set('connect()');
75
76         $f->exit(0);
77         $f->pipe(@mda);
78         $ok = $n + 1;
79         set_min($cfg, $ok);
80 }