public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 7228a3ad66de6298867932dfc2c81b3ca7b609aa 1745 bytes (raw)
$ git show HEAD:scripts/import_maildir	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 
#!/usr/bin/perl -w
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
=begin usage
Ancient script to import a Maildir into a v1 public-inbox

	# this is only if you want a v1 inbox
	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/

For v2 (strongly recommended), use:

	lei convert /path/to/maildir -o /path/to/v2-inbox
	# (and `lei daemon-kill' if you don't want the daemon to linger)
=cut
use v5.12;
use Date::Parse qw/str2time/;
use PublicInbox::Eml;
use PublicInbox::Git;
use PublicInbox::Import;
sub usage {
	open my $fh, '<', __FILE__;
	("Usage:\n", grep { /^=begin usage/../^=cut/ and !/^=/m } <$fh>);
}
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 = PublicInbox::Eml->new(do { 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::Eml->new(do { local $/; <$fh> });
	$im->add($mime);
}
$im->done;

1;

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git