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