#!/usr/bin/perl -w
# Copyright (C) 2015-2016 all contributors
# License: AGPL-3.0+
#
# Incremental (or one-shot) importer of a slrnpull news spool
=begin usage
export ORIGINAL_RECIPIENT=address@example.com
public-inbox-init $INBOX $GIT_DIR $HTTP_URL $ORIGINAL_RECIPIENT
./import_slrnspool SLRNPULL_ROOT/news/foo/bar
=cut
use strict;
use warnings;
use PublicInbox::Config;
use Email::MIME;
use PublicInbox::Import;
use PublicInbox::Git;
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 $ibx = $config->lookup($recipient);
my $git = $ibx->git;
my $im = PublicInbox::Import->new($git, $ibx->{name}, $ibx->{-primary_address});
sub key {
"publicinbox.$ibx->{name}.importslrnspoolstate";
}
sub get_min {
my $f = PublicInbox::Config->default_file;
my $out = $git->qx('config', "--file=$f", key($ibx));
$out ||= 0;
chomp $out;
$out =~ /\A\d+\z/ and return $out;
0;
}
sub set_min {
my ($num) = @_;
my $f = PublicInbox::Config->default_file;
my @cmd = (qw/git config/, "--file=$f", key($ibx), $num);
system(@cmd) == 0 or die join(' ', @cmd). " failed: $?\n";
}
my $n = get_min();
my $ok;
my $max_gap = 200000;
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 $mime = Email::MIME->new(eval { local $/; <$fh> });
my $hdr = $mime->header_obj;
# gmane rewrites Received headers, which increases spamminess
# Some older archives set Original-To
foreach my $x (qw(Received To)) {
my @h = $hdr->header_raw("Original-$x");
if (@h) {
$hdr->header_set($x, @h);
$hdr->header_set("Original-$x");
}
}
# Approved triggers for the SA HEADER_SPAM rule,
# X-From is gmane specific
foreach my $drop (qw(Approved X-From)) {
$hdr->header_set($drop);
}
# appears to be an old gmane bug:
$hdr->header_set('connect()');
$im->add($mime);
$ok = $n + 1;
set_min($ok);
}
$im->done;