# ------------------------------------------------------------------------------------------------------------------------------ # This perl script is designed to run through all of the mailboxes in /mnt/mail on # mail.visp.co.nz removing all corrupted data from the start of any corrupted # mailboxes. # # Coded By : Oliver Sneyd # When : February 2006 # Contact : oliver.sneyd@mail.iconz.net # ----------------------------------------------------------------------------------------------------------------------------- # Include the file statistics object so that the script can check the filesizes of each mailbox use File::stat; # Path to the maildir, /mnt/mail for mail.visp.co.nz $path = "./mail/"; $backupPath = "./backup/"; # Open up a directory handle print "\n\tGenerating mailbox list ...\n"; opendir(MAILDIR, $path); # Read the names of each entry in the maildir in to an arrays @filenames = readdir(MAILDIR); # Create an array to hold the mailboxes my @mailboxes = (); # Loop through the results returned by the directory handle for($i = 0; $i < @filenames; $i++) { # If the result returned by directory handle is NOT a directory if(not(-d ($path . $filenames[$i]))) { # Work out the file-size of the current mailbox $size = stat($path . $filenames[$i]); # If the filesize is greater than 0, add it to the mailbox list if($size->size > 0) { push(@mailboxes, $filenames[$i]); } } } # Loop through the mailboxes print "\tChecking for corrupt mailboxes ...\n\n"; while(@mailboxes > 0) { $mailbox = pop(@mailboxes); checkMailbox($mailbox); } print "\n\tDone.\n\n"; # Close the directory handle closedir(MAILDIR); # ------------------------------------------------------------ FUNCTIONS --------------------------------------------------- sub checkMailbox { # Set a corrupt variable to be true $corrupt = 1; # Loop untill corrupt is false $initial = 0; while($corrupt == 1) { # Open up the mailbox open(MAILBOX, ($path . $_[0])); # Read in the first line of the mailbox $line = ; # Get the index of the string "From" $idx = index($line, "From"); # If the index of "From" is 0, the mailbox isn't corrupted any more if($idx == 0) { # So set corrupted to false $corrupt = 0; } else { # Make a bacukp of the corrupted mailbox, just in case if($initial == 0) { print "\tFixing $_[0] ...\n"; system("cp $path" . $_[0] . " " . $backupPath . "."); $initial = 1; } # And remove the first line of the mailbox system("sed -e '1d' $path" . "$_[0] | more > $path" . $_[0]); } # Close the mailbox close(MAILBOX); } }