]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - scripts/splitmbox.pl
scripts: update to newer versions
[thirdparty/kernel/stable-queue.git] / scripts / splitmbox.pl
CommitLineData
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
8use Mail::Mbox::MessageParser;
9
10my ($mbox_file, $directory) = @ARGV;
11
12if ((not defined $mbox_file) or (not defined $directory)) {
13 print "splitmbox.pl mbox directory\n";
14 exit;
15}
16
17if (not -d $directory) {
18 print "directory $directory does not exist!\n";
19 exit;
20}
21
22my $mbox_reader = new Mail::Mbox::MessageParser( {
23 'file_name' => $mbox_file,
24 'enable_cache' => 0,
25 } );
26
27die $mbox_reader unless ref $mbox_reader;
28
29# compute number of messages in mailbox
30my $count = 0;
31while (!$mbox_reader->end_of_file()) {
32 my $email = $mbox_reader->read_next_email();
33 $count = $count + 1;
34}
35my $width_string = sprintf("%d", $count);
36my $width = length $width_string;
37print "mailbox count = $count\n";
38print "string width = $width\n";
39
40$mbox_reader->reset();
41
42$count = 0;
43while (!$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