]> Sergey Matveev's repositories - public-inbox.git/blob - scripts/import_maildir
269f2550fc94591d3ed227485154097671c045c0
[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 Date::Parse qw/str2time/;
15 use PublicInbox::Eml;
16 use PublicInbox::Git;
17 use PublicInbox::Import;
18 sub usage { "Usage:\n".join('', grep(/\t/, `head -n 24 $0`)) }
19 my $dir = shift @ARGV or die usage();
20 my $git_dir = `git rev-parse --git-dir`;
21 chomp $git_dir;
22 foreach my $sub (qw(cur new tmp)) {
23         -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n";
24 }
25
26 my @msgs;
27 foreach my $sub (qw(cur new)) {
28         foreach my $fn (glob("$dir/$sub/*")) {
29                 open my $fh, '<', $fn or next;
30                 my $s = PublicInbox::Eml->new(do { local $/; <$fh> });
31                 my $date = $s->header('Date');
32                 my $t = eval { str2time($date) };
33                 defined $t or next;
34                 my @fn = split(m!/!, $fn);
35                 push @msgs, [ $t, "$sub/" . pop @fn, $date ];
36         }
37 }
38
39 my $git = PublicInbox::Git->new($git_dir);
40 chomp(my $name = `git config user.name`);
41 chomp(my $email = `git config user.email`);
42 my $im = PublicInbox::Import->new($git, $name, $email);
43 @msgs = sort { $b->[0] <=> $a->[0] } @msgs;
44 while (my $ary = pop @msgs) {
45         my $fn = "$dir/$ary->[1]";
46         open my $fh, '<', $fn or next;
47         my $mime = PublicInbox::Eml->new(do { local $/; <$fh> });
48         $im->add($mime);
49 }
50 $im->done;
51
52 1;