2007-10-06 [colin] 3.0.2cvs20
[claws.git] / tools / convert_mbox.pl
1 #!/usr/bin/perl
2 # convert_mbox.pl
3 # perl script to convert mbox file to files in a new MH directory
4 # aka another mbox -> MH conversion tool
5 # 29 April 2003  
6 # Fred Marton <Fred.Marton@uni-bayreuth.de>
7
8 # Fixed (hopefully) to account for From lines
9 # that are of various length and that might have
10 # time zone info at the end
11 # 20 January 2004
12 #
13 # Note: Running this with the -w flag generates the following warnings:
14 # Scalar value @word[1] better written as $word[1] at /path/to/convert_mbox.pl line 54
15 # Scalar value @word[0] better written as $word[1] at /path/to/convert_mbox.pl line 56
16 # Making these changes requires further changes in the script
17 # that results in much longer run-times.  
18 #
19 # Copyright © 2003 Fred Marton
20
21 # This program is free software; you can redistribute it and/or
22 # modify it under the terms of the GNU General Public License
23 # as published by the Free Software Foundation; either version 3
24 # of the License, or (at your option) any later version.
25
26 # This program is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29 # GNU General Public License for more details.
30
31 # You should have received a copy of the GNU General Public License
32 # along with this program; if not, write to the Free Software
33 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
34 # 02111-1307, USA.
35
36 # check for both arguments
37 &usage if ($#ARGV < 1);
38 $mbox = $ARGV[0];
39 $mh = $ARGV[1];
40 # check to make sure there isn't something named MH already
41 if (-e $mh) {
42    die (" The directory \"$mh\" already exists.  Exiting.\n");
43 }
44 else {
45    mkdir $mh;
46 }
47 # start numbering
48 $i = 0;
49 # open the mbox file
50 open (IN, $mbox);
51 while ($line = <IN>) {
52 # check for the beginning of an e-mail
53    @word = split(/ +/m,$line);
54 # some lines might start with "From ", so check
55 # to see if the [second-to-]last word is a year
56    @word2 = split(/:/,$line);
57    chomp($word2[$#word2]);
58    @word3 = split(/ /,$word2[2]);
59    $year = @word3[1];
60 # ignore the MAILER-DAEMON message from pine
61    if (@word[1] ne "MAILER-DAEMON") {
62 # start a new file, assuming $year is > 1970
63       if (@word[0] eq "From" && $year > 1970) {
64          $i++;
65          close (OUT);
66          open (OUT, ">$mh/$i");
67          print OUT $line;
68       }
69       else {
70 # continue the file
71          print OUT $line;
72       }
73    }
74 }
75 close (OUT);
76 close (IN);
77 # and we're done
78 print "\n If it isn't there already, please move the directory \"$mh\"\n"
79     . " into your MH directory and rebuild your folder tree.\n\n";
80
81 sub usage
82 {
83    die ( " usage: convert_mbox.pl MBOX MH_DIR\n");
84 }