aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/splitmbox.pl
blob: 755da501f9ed46cc310029d590ba06ee0bc14ad7 (plain)
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
#!/usr/bin/perl
# perl version of splitmbox.py
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2022 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
#
# I didn't want to deal with the python2->3 transition so I rewrote it in perl...

use Mail::Mbox::MessageParser;

my ($mbox_file, $directory) = @ARGV;

if ((not defined $mbox_file) or (not defined $directory)) {
	print "splitmbox.pl mbox directory\n";
	exit;
}

if (not -d $directory) {
	print "directory $directory does not exist!\n";
	exit;
}

my $mbox_reader = new  Mail::Mbox::MessageParser( {
		'file_name' => $mbox_file,
		'enable_cache' => 0,
	} );

die $mbox_reader unless ref $mbox_reader;

# compute number of messages in mailbox
my $count = 0;
while (!$mbox_reader->end_of_file()) {
	my $email = $mbox_reader->read_next_email();
	$count = $count + 1;
}
my $width_string = sprintf("%d", $count);
my $width = length $width_string;
print "mailbox count = $count\n";
print "string width  = $width\n";

$mbox_reader->reset();

$count = 0;
while (!$mbox_reader->end_of_file()) {
	my $filename = sprintf("%s/msg.%0*d", $directory, $width, $count);
	my $email = $mbox_reader->read_next_email();

	print "filename = $filename\n";
	open(FILE, '>', $filename) or die $!;
	print FILE $$email;
	close(FILE);
	$count = $count + 1;
}