]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_vger_from_mbox
scripts/import_vger_from_mbox: use v2 layout for import
[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 Date::Parse qw/str2time/;
8 use Email::MIME;
9 $Email::MIME::ContentType::STRICT_PARAMS = 0; # user input is imperfect
10 use PublicInbox::Git;
11 use PublicInbox::Import;
12 my $usage = "usage: $0 NAME EMAIL <MBOX\n";
13 my $dry_run;
14 my %opts = ( 'n|dry-run' => \$dry_run );
15 GetOptions(%opts) or die $usage;
16 chomp(my $git_dir = `git rev-parse --git-dir`);
17 my $git = PublicInbox::Git->new($git_dir);
18 my $name = shift or die $usage; # git
19 my $email = shift or die $usage; # git@vger.kernel.org
20 my $im = $dry_run ? undef : PublicInbox::Import->new($git, $name, $email);
21 binmode STDIN;
22 my $msg = '';
23 use PublicInbox::Filter::Vger;
24 my $vger = PublicInbox::Filter::Vger->new;
25 if ($im) {
26         $im->{ssoma_lock} = 0;
27         $im->{path_type} = 'v2';
28 }
29
30 sub do_add ($$) {
31         my ($im, $msg) = @_;
32         $$msg =~ s/(\r?\n)+\z/$1/s;
33         $msg = Email::MIME->new($$msg);
34         $msg = $vger->scrub($msg);
35         return unless $im;
36         $im->add($msg) or
37                 warn "duplicate: ",
38                         $msg->header_obj->header_raw('Message-ID'), "\n";
39 }
40
41 # asctime: From example@example.com Fri Jun 23 02:56:55 2000
42 my $from_strict = qr/^From \S+ +\S+ \S+ +\S+ [^:]+:[^:]+:[^:]+ [^:]+/;
43 my $prev = undef;
44 while (defined(my $l = <STDIN>)) {
45         if ($l =~ /$from_strict/o) {
46                 if (!defined($prev) || $prev =~ /^\r?$/) {
47                         do_add($im, \$msg) if $msg;
48                         $msg = '';
49                         $prev = $l;
50                         next;
51                 }
52                 warn "W[$.] $l\n";
53         }
54         $prev = $l;
55         $msg .= $l;
56 }
57 do_add($im, \$msg) if $msg;
58 $im->done if $im;