about summary refs log tree commit homepage
path: root/scripts/import_gmane_spool
blob: 3cda0bf6d7dc8d2c5517347d103849627467012b (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
#!/usr/bin/perl -w
# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
# License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
#
# One-off script to convert an slrnpull news spool from gmane, usage:
=begin usage
	mkdir -p $HOME/.public-inbox
	MAINREPO=/path/to/your/repo.git
	export ORIGINAL_RECIPIENT='list@example.com'
	git init --bare $MAINREPO
	export GIT_CONFIG=$HOME/.public-inbox/config
	git config publicinbox.$LISTNAME.address $ORIGINAL_RECIPIENT
	git config publicinbox.$LISTNAME.mainrepo $MAINREPO
	unset GIT_CONFIG
	./import_gmane_spool SLRNPULL_ROOT/news/foo/bar
=cut
use strict;
use warnings;
use Email::Simple;
use PublicInbox::Filter;
use PublicInbox::Config;
use IPC::Run qw(run);
sub usage { "Usage:\n".join("",grep(/\t/, `head -n 24 $0`)) }
my $spool = shift @ARGV or die usage();
defined $ENV{ORIGINAL_RECIPIENT} or die usage();
my @args = ('public-inbox-mda');

chdir $spool or die "chdir $spool failed: $!\n";

foreach my $n (sort { $a <=> $b } grep(/\d+\z/, glob("*"))) {
	if (open my $fh, '<', $n) {
		my $s = eval {
			local $/;
			Email::Simple->new(<$fh>);
		};

		# gmane rewrites Received headers, which increases spamminess
		my @h = $s->header("Original-Received");
		if (@h) {
			$s->header_set("Received", @h);
			$s->header_set("Original-Received");
		}

		# this is needed for "git rev-list --since=..." to work
		local $ENV{GIT_COMMITTER_DATE} = $s->header('Date');

		# 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()");

		my $orig = $s->as_string;
		close $fh or die "close failed: $!\n";
		eval { run(\@args, \$orig) };
		die "fail $n: $?\n" if $?;
		die "fail $n: $@\n" if $@;
	} else {
		warn "Failed to open $n: $!\n";
	}
}