3 # perl script to convert mbox file to files in a new MH directory
4 # aka another mbox -> MH conversion tool
6 # Fred Marton <Fred.Marton@uni-bayreuth.de>
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
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.
19 # Copyright (c) 2003 Fred Marton
20 # Copyright (c) 2009 Ricardo Mones, based on the bash script
21 # by Daniel Dickinson [1]
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License
25 # as published by the Free Software Foundation; either version 3
26 # of the License, or (at your option) any later version.
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to the Free Software
35 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
38 # [1] http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=15;filename=convert_mbox.sh;att=1;bug=461435
44 &usage
if ($#ARGV < 1);
45 if ($ARGV[0] eq "-R") {
46 # recursive, need more args
47 &usage
if ($#ARGV < 2);
48 &usage
unless (-d
$ARGV[1]);
49 &usage
unless (-d
$ARGV[2]);
50 &convert_all
($ARGV[1], $ARGV[2]);
54 &convert_mbox
($ARGV[0], $$ARGV[1]);
58 # raw translation of bash, probably doable in real Perl better
62 while ($dirleft ne "." and $dirleft ne "") {
63 $enddir = &basename
($dirleft, ['.sbd']);
64 if ($nosbddir ne "") {
65 $nosbddir = "$enddir/$nosbddir";
70 $dirleft = &dirname
($dirleft);
76 ($mboxdir, $targetdir) = @_;
77 $tmpbase = '/tmp/frommbox';
78 $tmplist = '/tmp/.convert_mbox.filelist';
81 qx/find * -type -d -a ! -name '*.*'i | sort > $tmplist/;
82 open (FLH
, "<$tmplist") or die "cannot open $tmplist: $!\n";
85 &convert_one
($_, $targetdir, $tmpbase);
93 # targetdir isn't used in the original bash script, maybe a bug?
94 ($file, $targetdir, $tmpbase) = @_;
95 $dirname = &dirname
($file);
96 $filename = &basename
($file);
97 if ($dirname eq $filename) {
100 $dirname = &strip_sbd
($dirname);
101 if ($dirname ne "") {
102 $dirname = "/$dirname";
104 &mkpath
($tmpbase . $dirname);
105 $basefile = &basename
($file);
106 $base1 = &basename
($file, ['.cmeta']);
107 $base2 = &basename
($base1, ['.ibex.index']);
108 $base3 = &basename
($base2, ['.ibex.index.data']);
109 $base5 = &basename
($base3, ['.ev-summary']);
110 $base4 = &basename
($base5, ['.ev-summary-meta']);
111 $subdir = &basename
($dirname);
112 $basedir = "/" . &dirname
($dirname);
113 if ( $basedir eq '/.' or $basedir eq '/' ) {
117 $basedir = $tmpbase . $basedir;
120 if ( -f
"$basedir/$subdir" ) {
122 qx/mv \"$basedir\/$subdir\" \"$basedir\
/$subdir.tmp\"/;
123 &mkpath
("$basedir/$subdir");
125 if ( ! -d
"$basedir/$subdir/$base4" ) {
126 print "$basedir/$subdir/$base4\n";
127 &convert_mbox
($file, "$basedir/$subdir/$base4", 'quiet');
128 if ($dirisfile == 1) {
129 qx/mv \"$basedir\/$subdir.tmp\
/*\" \"$basedir\/$subdir\
/\"/;
136 ($mbox, $mh, $quiet) = @_;
137 # check to make sure there isn't something named MH already
139 die (" The directory \"$mh\" already exists. Exiting.\n");
148 while ($line = <IN
>) {
149 # check for the beginning of an e-mail
150 @word = split(/ +/m,$line);
151 # some lines might start with "From ", so check
152 # to see if the [second-to-]last word is a year
153 @word2 = split(/:/,$line);
154 chomp($word2[$#word2]);
155 @word3 = split(/ /,$word2[2]);
157 # ignore the MAILER-DAEMON message from pine
158 if (@word[1] ne "MAILER-DAEMON") {
159 # start a new file, assuming $year is > 1970
160 if (@word[0] eq "From" && $year > 1970) {
163 open (OUT
, ">$mh/$i");
175 if (! defined($quiet)) {
176 print "\n If it isn't there already, please move the directory \"$mh\"\n"
177 . " into your MH directory and rebuild your folder tree.\n\n";
182 die ("Usage: convert_mbox.pl MBOX MH_DIR\n"
183 . " convert_mbox.pl -R MBOXDIR MH_DIR\n"
185 . " -R Converts recursively a directory of mboxes\n");