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