#! /usr/bin/perl ############################################################################### # migratemail - migrate mail from mbox to maildir system # Copyright (C) 2000 Matthew M Lavy # mml@mupsych.org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Usage: # migratemail.pl # # where is the path to the OLD mbox-style inbox # is a directory full of mboxes (or a directory # of directories of mboxes... # is the directory within which Maildir will be created # will have Maildir chowned to them # will have Maildir chgrped to them # ############################################################################### # CHANGE THIS TO POINT TO mbox2maildir ON YOUR SYSTEM: $mbox2maildir='/usr/local/sbin/mbox2maildir'; # CHANGE THIS TO POINT TO GNU touch ON YOUR SYSTEM: $touch_cmd='/usr/bin/touch'; ############################################################################### # NO CHANGES SHOULD BE NECESSARY BELOW THIS LINE ############################################################################### #################################### # performs VERY simple test to see if # a file is likely to be an mbox #################################### sub is_mbox($) { open(FILE,$_[0]) || print STDERR "Failed to open mbox on $user - $_[0]"; my $testline = ; close FILE; return ($testline =~ /^From\s.*/); } ################################## # recursively descends a directory # structure, building a flat list of # subfolders to create ################################## sub getSubfolders() { my $pwd = $_[0]; my @folderlist = (); opendir(DIR,"$oldhome/$pwd") || return(0); @files = readdir(DIR); closedir DIR; foreach (@files){ if (! /^\.+$/){ if ( -d "$oldhome/$pwd/$_"){ push @folderlist, getSubfolders("$pwd/$_"); } else { if (is_mbox("$oldhome/$pwd/$_")){ push @folderlist, "$pwd/$_"; } } } } return @folderlist; } # script proper starts here if(@ARGV != 5){ print "Usage: migratemail \n"; exit (1); } ($oldinbox, $oldhome, $newhome, $user, $group) = @ARGV; # Save time (and, ok, allow for potential race condition) # by doing the basic sanity checks here unless ( -f $oldinbox && is_mbox($oldinbox)){ print " must point to an mbox file.\n"; exit (1); } unless ( -d $oldhome){ print " must point to a valid directory.\n"; exit (1); } unless ( -d $newhome){ print " must point to a valid directory.\n"; exit (1); } unless ($uid = getpwnam($user)){ print " must point to a valid user.\n"; exit (1); } unless ($gid = getgrnam($group)){ print " must point to a valid group.\n"; exit (1); } print "Creating Maildir for $user\n"; # Now create the Maildir from inbox using mbox2maildir # Hopefully, it is now fairly unlikely to fail unless I am running the # script under incredibly stupid circumstances if (system("$mbox2maildir $oldinbox $newhome/Maildir")){ print "mbox2maildir failed to create new Maildir.\n"; exit (1); } print " - Maildir $newhome/Maildir created successfully\n"; # Trawl through the homespace hierarchy, creating Maildir subfolders for each # file found. push @subfolders, getSubfolders(); foreach (@subfolders){ my $fulloldpath = "$oldhome/$_"; tr/\//\./; #dashes to dots my $thesubfolder = $_; # enclose the params in literal quotes to cope with spaces in e.g. Sent Items if (system("$mbox2maildir \"$fulloldpath\" \"$newhome/Maildir/$thesubfolder\"")){ print " --- failed to create $thesubfolder.\n"; } else { # make it a maildirsubfolder - this CANNOT fail here!!! system("$touch_cmd \"$newhome/Maildir/$thesubfolder/maildirfolder\""); print " --- successfully created $thesubfolder.\n"; } } # Finally, chown the whole lot to the user and group provided # (chmodding has been done by mailmakedir) # Use system chown with recursive option, as this is easiest plan... if (system("chown -R $uid:$gid $newhome/Maildir")){ print "Unable to set ownership for new Maildir\n"; } else { print "Change of ownership completed successfully\n"; }