about summary refs log tree commit homepage
path: root/scripts/import_slrnspool
blob: e55c96c7dcde95b376db44f5934bf96fe4d22286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/perl -w
# Copyright (C) 2015, all contributors <meta@public-inbox.org>
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
#
# Incremental (or one-shot) importer of a slrnpull news spool
=begin usage
	export ORIGINAL_RECIPIENT=address@example.com
	public-inbox-init $LISTNAME $GIT_DIR $HTTP_URL $ORIGINAL_RECIPIENT
	./import_slrnspool SLRNPULL_ROOT/news/foo/bar
=cut
use strict;
use warnings;
use PublicInbox::Config;
use Email::Filter;
use Email::LocalDelivery;
sub usage { "Usage:\n".join('',grep(/\t/, `head -n 10 $0`)) }
my $exit = 0;
my $sighandler = sub { $exit = 1 };
$SIG{INT} = $sighandler;
$SIG{TERM} = $sighandler;
my $spool = shift @ARGV or die usage();
my $recipient = $ENV{ORIGINAL_RECIPIENT};
defined $recipient or die usage();
my $config = PublicInbox::Config->new;
my $cfg = $config->lookup($recipient);
defined $cfg or exit(1);
my @mda;
if ($ENV{'FILTER'}) {
	@mda = qw(public-inbox-mda);
} else {
	@mda = (qw(ssoma-mda -1), $cfg->{mainrepo});
}

sub key {
	my ($cfg) = @_;
	"publicinbox.$cfg->{listname}.importslrnspoolstate";
}

sub get_min {
	my $f = PublicInbox::Config->default_file;
	my @cmd = (qw/git config/, "--file=$f", key($cfg));
	use IPC::Run qw/run/;

	my $in = '';
	my $out = '';
	unless (run(\@cmd, \$in, \$out)) {
		$out = 0;
	}
	int($out);
}

sub set_min {
	my ($cfg, $num) = @_;
	my $f = PublicInbox::Config->default_file;
	my @cmd = (qw/git config/, "--file=$f", key($cfg), $num);
	system(@cmd) == 0 or die join(' ', @cmd). " failed: $?\n";
}

my $n = get_min();
my $ok;
my $max_gap = 10000;
my $max = $n + $max_gap;

for (; $exit == 0 && $n < $max; $n++) {
	my $fn = "$spool/$n";
	print STDERR $fn, "\n";
	open(my $fh, '<', $fn) or next;
	$max = $n + $max_gap;
	my $f = Email::Filter->new(data => eval { local $/; <$fh> });
	my $s = $f->simple;

	# gmane rewrites Received headers, which increases spamminess
	# Some older archives set Original-To
	foreach my $x (qw(Received To)) {
		my @h = $s->header("Original-$x");
		if (@h) {
			$s->header_set($x, @h);
			$s->header_set("Original-$x");
		}
	}

	# triggers for the SA HEADER_SPAM rule
	foreach my $drop (qw(Approved)) { $s->header_set($drop) }

	# appears to be an old gmane bug:
	$s->header_set('connect()');

	$f->exit(0);
	$f->pipe(@mda);
	$ok = $n + 1;
	set_min($cfg, $ok);
}