smeserver-email/root/etc/e-smith/events/actions/purge-junkmail-folders

68 lines
1.5 KiB
Perl

#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 1999-2007 Mitel Networks Corporation
#----------------------------------------------------------------------
use strict;
use esmith::ConfigDB;
use esmith::AccountsDB;
use User::pwent;
use File::Find;
sub purgedir;
my $db = esmith::ConfigDB->open_ro or
die("Could not open config db");;
my $adb = esmith::AccountsDB->open_ro or
die("Could not open accounts db");;
my $sa = $db->get('spamassassin') or
die("Could not get spamassassin record from config db\n");
my $age = $sa->prop('MessageRetentionTime') || '0';
exit 0 unless $age;
my @users = ("admin", map { $_->key } $adb->users);
foreach my $user ( @users )
{
my $pwent = getpwnam($user)
or die "Couldn't get password entry for $user\n";
chdir $pwent->dir or die "Couldn't chdir " . $pwent->dir . ":$!\n";
unless (chdir "Maildir/.junkmail")
{
warn("Couldn't chdir to junkmail folder of user $user: $!\n");
next;
}
foreach my $dir (qw(new tmp cur))
{
if (-d $dir)
{
purgedir($dir, $age);
}
else
{
warn("Somebody has been fiddling - ~$user/Maildir/.junkmail/$dir is missing\n");
}
}
}
exit 0;
sub purgedir
{
# Unlink all files in dir $dir which are more than $age days old
my ($dir, $age) = @_;
use DirHandle;
my $dh = DirHandle->new($dir);
unlink grep { -M > $age } # choose files older than $age days
grep { -f } # Choose only plain files
map { "$dir/$_" } # create full paths
$dh->read;
}