#!/usr/bin/perl -w # Copyright (C) 2014, Eric Wong and all contributors # License: AGPL-3.0+ # # Script to import a Maildir into a public-inbox =begin usage export GIT_DIR=/path/to/your/repo.git export GIT_AUTHOR_EMAIL='list@example.com' export GIT_AUTHOR_NAME='list name' ./import_maildir /path/to/maildir/ =cut use strict; use warnings; use Email::Simple; use Date::Parse qw/str2time/; use PublicInbox::MIME; use PublicInbox::Git; use PublicInbox::Import; sub usage { "Usage:\n".join('', grep(/\t/, `head -n 24 $0`)) } my $dir = shift @ARGV or die usage(); my $git_dir = `git rev-parse --git-dir`; chomp $git_dir; foreach my $sub (qw(cur new tmp)) { -d "$dir/$sub" or die "$dir is not a Maildir (missing $sub)\n"; } my @msgs; foreach my $sub (qw(cur new)) { foreach my $fn (glob("$dir/$sub/*")) { open my $fh, '<', $fn or next; my $s = Email::Simple->new(eval { local $/; <$fh> }); my $date = $s->header('Date'); my $t = eval { str2time($date) }; defined $t or next; my @fn = split(m!/!, $fn); push @msgs, [ $t, "$sub/" . pop @fn, $date ]; } } my $git = PublicInbox::Git->new($git_dir); chomp(my $name = `git config user.name`); chomp(my $email = `git config user.email`); my $im = PublicInbox::Import->new($git, $name, $email); @msgs = sort { $b->[0] <=> $a->[0] } @msgs; while (my $ary = pop @msgs) { my $fn = "$dir/$ary->[1]"; open my $fh, '<', $fn or next; my $mime = PublicInbox::MIME->new(eval { local $/; <$fh> }); $im->add($mime); } $im->done; 1;