about summary refs log tree commit homepage
path: root/scripts/import_vger_from_mbox
blob: 3fa5c77891040e2a3be3c6e6c15d03545d821323 (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
#!/usr/bin/perl -w
# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict;
use warnings;
use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
use Date::Parse qw/str2time/;
use Email::MIME;
$Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
use PublicInbox::Git;
use PublicInbox::Import;
my $usage = "usage: $0 NAME EMAIL <MBOX\n";
my $dry_run;
my %opts = ( 'n|dry-run' => \$dry_run );
GetOptions(%opts) or die $usage;
chomp(my $git_dir = `git rev-parse --git-dir`);
my $git = PublicInbox::Git->new($git_dir);
my $name = shift or die $usage; # git
my $email = shift or die $usage; # git@vger.kernel.org
my $im = $dry_run ? undef : PublicInbox::Import->new($git, $name, $email);
binmode STDIN;
my $msg = '';
use PublicInbox::Filter::Vger;
my $vger = PublicInbox::Filter::Vger->new;
sub do_add ($$) {
	my ($im, $msg) = @_;
	$$msg =~ s/(\r?\n)+\z/$1/s;
	$msg = Email::MIME->new($$msg);
	$msg = $vger->scrub($msg);
	my $hdr = $msg->header_obj;
	my $date = $hdr->header_raw('Date');
	if ($date) {
		eval { str2time($date) };
		if ($@) {
			warn "bad Date: $date in ",
				$hdr->header_raw('Message-ID'), ": $@\n";
		}
	} else {
		warn "missing Date: $date in ",
				$hdr->header_raw('Message-ID'), ": $@\n";
		my $n = 0;
		foreach my $r ($hdr->header_raw('Received')) {
			warn "$n Received: $r\n";
		}
		warn(('-' x 72), "\n");
	}
	return unless $im;
	$im->add($msg) or
		warn "duplicate: ",
			$hdr->header_raw('Message-ID'), "\n";
}

# asctime: From example@example.com Fri Jun 23 02:56:55 2000
my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
my $prev = undef;
while (defined(my $l = <STDIN>)) {
	if ($l =~ /$from_strict/o) {
		if (!defined($prev) || $prev =~ /^\r?$/) {
			do_add($im, \$msg) if $msg;
			$msg = '';
			$prev = $l;
			next;
		}
		warn "W[$.] $l\n";
	}
	$prev = $l;
	$msg .= $l;
}
do_add($im, \$msg) if $msg;
$im->done if $im;