]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_vger_from_mbox
favor Received: date over Date: header globally
[public-inbox.git] / scripts / import_vger_from_mbox
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use warnings;
6 use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
7 use PublicInbox::MIME;
8 use PublicInbox::Inbox;
9 use PublicInbox::V2Writable;
10 use PublicInbox::Import;
11 my $usage = "usage: $0 NAME EMAIL DIR <MBOX\n";
12 my $dry_run;
13 my $version = 2;
14 my %opts = (
15         'n|dry-run' => \$dry_run,
16         'V|version=i' => \$version,
17 );
18 GetOptions(%opts) or die $usage;
19 my $name = shift or die $usage; # git
20 my $email = shift or die $usage; # git@vger.kernel.org
21 my $mainrepo = shift or die $usage; # /path/to/v2/repo
22 my $ibx = {
23         mainrepo => $mainrepo,
24         name => $name,
25         version => $version,
26         -primary_address => $email,
27 };
28 $ibx = PublicInbox::Inbox->new($ibx);
29 my $im;
30 unless ($dry_run) {
31         if ($version >= 2) {
32                 $im = PublicInbox::V2Writable->new($ibx, 1);
33         } else {
34                 system(qw(git init --bare -q), $mainrepo);
35                 my $git = PublicInbox::Git->new($mainrepo);
36                 $im = PublicInbox::Import->new($git, $name, $email, $ibx);
37         }
38 }
39 binmode STDIN;
40 my $msg = '';
41 use PublicInbox::Filter::Vger;
42 my $vger = PublicInbox::Filter::Vger->new;
43
44 sub do_add ($$) {
45         my ($im, $msg) = @_;
46         $$msg =~ s/(\r?\n)+\z/$1/s;
47         my $mime = PublicInbox::MIME->new($msg);
48         $mime = $vger->scrub($mime);
49         return unless $im;
50         $im->add($mime) or
51                 warn "duplicate: ",
52                         $mime->header_obj->header_raw('Message-ID'), "\n";
53 }
54
55 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
56 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
57 my $prev = undef;
58 while (defined(my $l = <STDIN>)) {
59         if ($l =~ /$from_strict/o) {
60                 if (!defined($prev) || $prev =~ /^\r?$/) {
61                         do_add($im, \$msg) if $msg;
62                         $msg = '';
63                         $prev = $l;
64                         next;
65                 }
66                 warn "W[$.] $l\n";
67         }
68         $prev = $l;
69         $msg .= $l;
70 }
71 do_add($im, \$msg) if $msg;
72 $im->done if $im;