convert_mbox.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 2
  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. # check for both arguments
  36. &usage if ($#ARGV < 1);
  37. $mbox = $ARGV[0];
  38. $mh = $ARGV[1];
  39. # check to make sure there isn't something named MH already
  40. if (-e $mh) {
  41. die (" The directory \"$mh\" already exists. Exiting.\n");
  42. }
  43. else {
  44. mkdir $mh;
  45. }
  46. # start numbering
  47. $i = 0;
  48. # open the mbox file
  49. open (IN, $mbox);
  50. while ($line = <IN>) {
  51. # check for the beginning of an e-mail
  52. @word = split(/ +/m,$line);
  53. # some lines might start with "From ", so check
  54. # to see if the [second-to-]last word is a year
  55. @word2 = split(/:/,$line);
  56. chomp($word2[$#word2]);
  57. @word3 = split(/ /,$word2[2]);
  58. $year = @word3[1];
  59. # ignore the MAILER-DAEMON message from pine
  60. if (@word[1] ne "MAILER-DAEMON") {
  61. # start a new file, assuming $year is > 1970
  62. if (@word[0] eq "From" && $year > 1970) {
  63. $i++;
  64. close (OUT);
  65. open (OUT, ">$mh/$i");
  66. print OUT $line;
  67. }
  68. else {
  69. # continue the file
  70. print OUT $line;
  71. }
  72. }
  73. }
  74. close (OUT);
  75. close (IN);
  76. # and we're done
  77. print "\n If it isn't there already, please move the directory \"$mh\"\n"
  78. . " into your MH directory and rebuild your folder tree.\n\n";
  79. sub usage
  80. {
  81. die ( " usage: convert_mbox.pl MBOX MH_DIR\n");
  82. }