I have been working on a perl script that will alert users when they are
approaching their mailbox size limit. The script "limit.pl" has basic
functionality, but still needs a few more features. It currently uses the
line "$max_dir_size = 5000000;" to set the limit in bytes. It issues a
warning based on the equation "$notify_dir_size = $max_dir_size - 1000000;".
Eventually, I intend to pull the "$max_dir_size" variable from the default
maximum size in the registry.

The size must be between $notify_dir_size and $max_dir_size for a warning to
be issued.

This program has not yet been tested in a production environment, so use at
your own risk. I submit the following script for review. Any comments or
suggestions for improvements are appreciated. 

----------------------------------------------------------------------------
-------------------

sub dir_size {
# Get the size of directory tree under Windows
# Take the dir of directory with the wide (/w) option to get a shorter list 
# and /s option to get all sub dirs  
my $test_dir = $_[0];
@res = `dir /s/w "$test_dir"`;
# The second to last line has the number of files and                     
# total size of the the directory
my $LastLine = $res[-2];
# Isolate directory size number
@res = split(/ /,$LastLine);
#remove commas;
$_ = $res[-2];
tr/0-9//cd;
$res[-2] = $_;
return $res[-2];
}

sub dir_list {
my $test_dir = $_[0];
#Issue "dir /ad" for directory list and assign results to @lines
@lines = `dir /ad "$test_dir"`;
#Remove first two lines, and last 7 lines
pop(@lines);
pop(@lines);
shift(@lines);
shift(@lines);
shift(@lines);
shift(@lines);
shift(@lines);
shift(@lines);
shift(@lines);
#Loop through each line of directory output and strip out everything 
#but the directory then call dir_size.
#assign @lines to $line, one at a time
foreach $line (@lines)                
{
@line = split(/ /,$line,21);
splice(@line,0,20);
chomp(@line);
my $current_dir = $line[0];
push (@list, $current_dir);
}
return @list;
}

sub get_reg_value {
local($hkey);
local($myhash);
$reg_path = 'SOFTWARE\Ipswitch\IMail\Global';
$key = $_[0];
$HKEY_LOCAL_MACHINE->Open($reg_path, $hkey) || die $!;
#$hkey->Open($reg_path, $hkey) || die $!;
$hkey->GetValues($myhash) || die $!;
return $myhash->{$key}[2];
}


#Main Program
use Win32::Registry;
$users_dir_path = get_reg_value('UsersDir');
$max_dir_size = 5000000;
#$notify_dir_size = 4000000;
$notify_dir_size = $max_dir_size - 1000000;

$imail = get_reg_value('TopDir') . "\\imail1.exe";
$temp_msg = $ENV{"TEMP"} . "\\outmail$$.txt";
$subject = 'Mailbox Size Limit Warning!';

chdir($users_dir_path);
@dirs = dir_list($users_dir_path);
foreach $dir (@dirs)
{
print "The directory " . $dir . " is " . dir_size($dir) . " bytes in size" .
"\n";
if (dir_size($dir) > $notify_dir_size){

$toaddress = $dir .'@localhost';
# Create A temporary File to construct the e-mail
open (MAIL, ">$temp_msg") || die "open postmail or sendmail";

# Construct the e-mail
print MAIL "You are approaching the allocated mailbox size limit of " .
$max_dir_size . " bytes." . "\n";
print MAIL "Your current mailbox size is " . dir_size($dir) . " bytes.\n\n";
print MAIL "You must maintain your mailbox by deleting mesages to keep your
mailbox below the limit.\n";
print MAIL "If you excede the limit, you will no longer receive email until
the mailbox is below the limit.\n";

# Finish writing to file
close MAIL;
system ("$imail -f $temp_msg -s $subject -t $toaddress");

# Remove the File
unlink $temp_msg;

}
}

----------------------------------------------------------------------------
-------------------
Please visit http://www.ipswitch.com/support/mailing-lists.html 
to be removed from this list.

Reply via email to