convert_mbox.pl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 (c) 2003 Fred Marton
  20. # Copyright (c) 2009 Ricardo Mones, based on the bash script
  21. # by Daniel Dickinson [1]
  22. #
  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.
  27. #
  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.
  32. #
  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
  36. # 02111-1307, USA.
  37. #
  38. # [1] http://bugs.debian.org/cgi-bin/bugreport.cgi?msg=15;filename=convert_mbox.sh;att=1;bug=461435
  39. use File::Path;
  40. use File::Basename;
  41. # check for arguments
  42. &usage if ($#ARGV < 1);
  43. if ($ARGV[0] eq "-R") {
  44. # recursive, need more args
  45. &usage if ($#ARGV < 2);
  46. &usage unless (-d $ARGV[1]);
  47. &usage unless (-d $ARGV[2]);
  48. &convert_all ($ARGV[1], $ARGV[2]);
  49. }
  50. else {
  51. # default behaviour
  52. &convert_mbox ($ARGV[0], $$ARGV[1]);
  53. }
  54. sub strip_sbd {
  55. # raw translation of bash, probably doable in real Perl better
  56. $dirname = shift;
  57. $nosbddir = "";
  58. $dirleft = $dirname;
  59. while ($dirleft ne "." and $dirleft ne "") {
  60. $enddir = &basename ($dirleft, ['.sbd']);
  61. if ($nosbddir ne "") {
  62. $nosbddir = "$enddir/$nosbddir";
  63. }
  64. else {
  65. $nosbddir = $enddir;
  66. }
  67. $dirleft = &dirname ($dirleft);
  68. }
  69. print "$nosbddir\n";
  70. }
  71. sub convert_all {
  72. ($mboxdir, $targetdir) = @_;
  73. $tmpbase = '/tmp/frommbox';
  74. $tmplist = '/tmp/.convert_mbox.filelist';
  75. $curdir = qx/pwd/;
  76. chdir ($mboxdir);
  77. qx/find * -type -d -a ! -name '*.*'i | sort > $tmplist/;
  78. open (FLH, "<$tmplist") or die "cannot open $tmplist: $!\n";
  79. while (<FLH>) {
  80. chomp;
  81. &convert_one ($_, $targetdir, $tmpbase);
  82. }
  83. close (FLH);
  84. unlink ($tmplist);
  85. chdir ($curdir);
  86. }
  87. sub convert_one {
  88. # targetdir isn't used in the original bash script, maybe a bug?
  89. ($file, $targetdir, $tmpbase) = @_;
  90. $dirname = &dirname ($file);
  91. $filename = &basename ($file);
  92. if ($dirname eq $filename) {
  93. $dirname = "";
  94. }
  95. $dirname = &strip_sbd ($dirname);
  96. if ($dirname ne "") {
  97. $dirname = "/$dirname";
  98. }
  99. &mkpath ($tmpbase . $dirname);
  100. $basefile = &basename ($file);
  101. $base1 = &basename ($file, ['.cmeta']);
  102. $base2 = &basename ($base1, ['.ibex.index']);
  103. $base3 = &basename ($base2, ['.ibex.index.data']);
  104. $base5 = &basename ($base3, ['.ev-summary']);
  105. $base4 = &basename ($base5, ['.ev-summary-meta']);
  106. $subdir = &basename ($dirname);
  107. $basedir = "/" . &dirname ($dirname);
  108. if ( $basedir eq '/.' or $basedir eq '/' ) {
  109. $basedir = $dirname;
  110. $subdir = "";
  111. }
  112. $basedir = $tmpbase . $basedir;
  113. &mkpath ($basedir);
  114. $dirisfile = 0;
  115. if ( -f "$basedir/$subdir" ) {
  116. $dirisfile = 1;
  117. qx/mv \"$basedir\/$subdir\" \"$basedir\/$subdir.tmp\"/;
  118. &mkpath ("$basedir/$subdir");
  119. }
  120. if ( ! -d "$basedir/$subdir/$base4" ) {
  121. print "$basedir/$subdir/$base4\n";
  122. &convert_mbox ($file, "$basedir/$subdir/$base4", 'quiet');
  123. if ($dirisfile == 1) {
  124. qx/mv \"$basedir\/$subdir.tmp\/*\" \"$basedir\/$subdir\/\"/;
  125. }
  126. }
  127. }
  128. sub convert_mbox {
  129. ($mbox, $mh, $quiet) = @_;
  130. # check to make sure there isn't something named MH already
  131. if (-e $mh) {
  132. die (" The directory \"$mh\" already exists. Exiting.\n");
  133. }
  134. else {
  135. mkdir $mh;
  136. }
  137. # start numbering
  138. $i = 0;
  139. # open the mbox file
  140. open (IN, $mbox);
  141. while ($line = <IN>) {
  142. # check for the beginning of an e-mail
  143. @word = split(/ +/m,$line);
  144. # some lines might start with "From ", so check
  145. # to see if the [second-to-]last word is a year
  146. @word2 = split(/:/,$line);
  147. chomp($word2[$#word2]);
  148. @word3 = split(/ /,$word2[2]);
  149. $year = @word3[1];
  150. # ignore the MAILER-DAEMON message from pine
  151. if (@word[1] ne "MAILER-DAEMON") {
  152. # start a new file, assuming $year is > 1970
  153. if (@word[0] eq "From" && $year > 1970) {
  154. $i++;
  155. close (OUT);
  156. open (OUT, ">$mh/$i");
  157. print OUT $line;
  158. }
  159. else {
  160. # continue the file
  161. print OUT $line;
  162. }
  163. }
  164. }
  165. close (OUT);
  166. close (IN);
  167. # and we're done
  168. if (! defined($quiet)) {
  169. print "\n If it isn't there already, please move the directory \"$mh\"\n"
  170. . " into your MH directory and rebuild your folder tree.\n\n";
  171. }
  172. }
  173. sub usage {
  174. die ("Usage: convert_mbox.pl MBOX MH_DIR\n"
  175. . " convert_mbox.pl -R MBOXDIR MH_DIR\n"
  176. . "Where: \n"
  177. . " -R Converts recursively a directory of mboxes\n");
  178. }