]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_maildir
public-inbox 1.1.0-pre1
[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: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Script to import a Maildir into a public-inbox
6 =begin usage
7         export GIT_DIR=/path/to/your/repo.git
8         export GIT_AUTHOR_EMAIL='list@example.com'
9         export GIT_AUTHOR_NAME='list name'
10         ./import_maildir /path/to/maildir/
11 =cut
12 use strict;
13 use warnings;
14 use Email::Simple;
15 use Date::Parse qw/str2time/;
16 use PublicInbox::MIME;
17 use PublicInbox::Git;
18 use PublicInbox::Import;
19 sub usage { "Usage:\n".join('', grep(/\t/, `head -n 24 $0`)) }
20 my $dir = shift @ARGV or die usage();
21 my $git_dir = `git rev-parse --git-dir`;
22 chomp $git_dir;
23 foreach my $sub (qw(cur new tmp)) {
24         -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
25 }
26
27 my @msgs;
28 foreach my $sub (qw(cur new)) {
29         foreach my $fn (glob("$dir/$sub/*")) {
30                 open my $fh, '<', $fn or next;
31                 my $s = Email::Simple->new(eval { local $/; <$fh> });
32                 my $date = $s->header('Date');
33                 my $t = eval { str2time($date) };
34                 defined $t or next;
35                 my @fn = split(m!/!, $fn);
36                 push @msgs, [ $t, "$sub/" . pop @fn, $date ];
37         }
38 }
39
40 my $git = PublicInbox::Git->new($git_dir);
41 chomp(my $name = `git config user.name`);
42 chomp(my $email = `git config user.email`);
43 my $im = PublicInbox::Import->new($git, $name, $email);
44 @msgs = sort { $b->[0] <=> $a->[0] } @msgs;
45 while (my $ary = pop @msgs) {
46         my $fn = "$dir/$ary->[1]";
47         open my $fh, '<', $fn or next;
48         my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> });
49         $im->add($mime);
50 }
51 $im->done;
52
53 1;