]>
Commit | Line | Data |
---|---|---|
f3cb88dc GKH |
1 | #!/usr/bin/perl |
2 | # perl version of splitmbox.py | |
3 | # SPDX-License-Identifier: GPL-2.0 | |
4 | # Copyright (c) 2022 Greg Kroah-Hartman <gregkh@linuxfoundation.org> | |
5 | # | |
6 | # I didn't want to deal with the python2->3 transition so I rewrote it in perl... | |
7 | ||
8 | use Mail::Mbox::MessageParser; | |
9 | ||
10 | my ($mbox_file, $directory) = @ARGV; | |
11 | ||
12 | if ((not defined $mbox_file) or (not defined $directory)) { | |
13 | print "splitmbox.pl mbox directory\n"; | |
14 | exit; | |
15 | } | |
16 | ||
17 | if (not -d $directory) { | |
18 | print "directory $directory does not exist!\n"; | |
19 | exit; | |
20 | } | |
21 | ||
22 | my $mbox_reader = new Mail::Mbox::MessageParser( { | |
23 | 'file_name' => $mbox_file, | |
24 | 'enable_cache' => 0, | |
25 | } ); | |
26 | ||
27 | die $mbox_reader unless ref $mbox_reader; | |
28 | ||
29 | # compute number of messages in mailbox | |
30 | my $count = 0; | |
31 | while (!$mbox_reader->end_of_file()) { | |
32 | my $email = $mbox_reader->read_next_email(); | |
33 | $count = $count + 1; | |
34 | } | |
35 | my $width_string = sprintf("%d", $count); | |
36 | my $width = length $width_string; | |
37 | print "mailbox count = $count\n"; | |
38 | print "string width = $width\n"; | |
39 | ||
40 | $mbox_reader->reset(); | |
41 | ||
42 | $count = 0; | |
43 | while (!$mbox_reader->end_of_file()) { | |
44 | my $filename = sprintf("%s/msg.%0*d", $directory, $width, $count); | |
45 | my $email = $mbox_reader->read_next_email(); | |
46 | ||
47 | print "filename = $filename\n"; | |
48 | open(FILE, '>', $filename) or die $!; | |
49 | print FILE $$email; | |
50 | close(FILE); | |
51 | $count = $count + 1; | |
52 | } | |
53 | ||
54 |