]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_maildir
new scripts for importing slrn spools and maildirs
[public-inbox.git] / scripts / import_maildir
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014, Eric Wong <e@80x24.org> and all contributors
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 #
5 # Script to import a Maildir into a public-inbox
6 =begin usage
7         mkdir -p $HOME/.public-inbox
8         MAINREPO=/path/to/your/repo.git
9         export ORIGINAL_RECIPIENT='list@example.com'
10         git init --bare $MAINREPO
11         export GIT_CONFIG=$HOME/.public-inbox/config
12         git config publicinbox.$LISTNAME.address $ORIGINAL_RECIPIENT
13         git config publicinbox.$LISTNAME.mainrepo $MAINREPO
14         unset GIT_CONFIG
15         ./import_maildir /path/to/maildir/
16 =cut
17 use strict;
18 use warnings;
19 use Email::Filter;
20 use Date::Parse qw/str2time/;
21 use IPC::Run qw/run/;
22 sub usage { "Usage:\n".join('', grep(/\t/, `head -n 24 $0`)) }
23 my $dir = shift @ARGV or die usage();
24 defined $ENV{ORIGINAL_RECIPIENT} or die usage();
25 my @mda = qw(public-inbox-mda);
26 foreach my $sub (qw(cur new tmp)) {
27         -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
28 }
29
30 my @msgs;
31 foreach my $sub (qw(cur new)) {
32         foreach my $fn (glob("$dir/$sub/*")) {
33                 open my $fh, '<', $fn or next;
34                 my $f = Email::Filter->new(data => eval { local $/; <$fh> });
35                 my $date = $f->simple->header('Date');
36                 my $t = eval { str2time($date) };
37                 $f->exit(0);
38                 $f->ignore;
39                 defined $t or next;
40                 my @fn = split(m!/!, $fn);
41                 push @msgs, [ $t, "$sub/" . pop @fn, $date ];
42         }
43 }
44
45 @msgs = sort { $b->[0] <=> $a->[0] } @msgs;
46 while (my $ary = pop @msgs) {
47         my $fn = "$dir/$ary->[1]";
48         local $ENV{GIT_COMMITTER_DATE} = $ary->[2]; # this preserves timezone
49         run(\@mda, '<', $fn);
50 }
51
52 1;