#!/usr/local/bin/perl -w use strict; use File::Find; use DBI; my $dbh = DBI->connect('dbi:mysql:database=Accounts;host=localhost', 'user', 'pass') or die "Can't connect to DB: ".$DBI::errstr."\n"; my $sql = "SELECT value from SA_userprefs where username=? and preference='x-spam-days';"; my $sth = $dbh->prepare($sql) or die "Can't prepare SQL: ".$dbh->errstr."\n"; $| = 1; sub DEBUG { 0 }; my $spam_log = '/usr/local/www/cgi-data/spam.log'; my @args = @ARGV; my $dry_run = 0; my $default_stale_days = 14; for (my $i = 0; $i < @args; $i++) { if ($args[$i] eq '-n') { $dry_run = 1; } elsif ($args[$i] eq '-d') { $default_stale_days = $args[$i+1]; } } $default_stale_days = 14 if not $default_stale_days or $default_stale_days =~ /\D/; my $maildir = pop(@args) || '/var/mail/virtual'; print "Dry run: Not doing anything\n" if $dry_run; my $spam_killed = 0; my $spam_killed_size = 0; my $spam_total = 0; my $spam_total_size = 0; clean_spam(); print "Total spam: $spam_total\n"; print "Total size: $spam_total_size bytes (", bytes_to_human($spam_total_size, 'm'), " M)\n"; print "Deleted: $spam_killed\n"; print "Deleted size: $spam_killed_size bytes (", bytes_to_human($spam_killed_size, 'm'), " M)\n"; if ($spam_log and not $dry_run) { open (LOG, ">>$spam_log") or die "Can't append to $spam_log: $!\n"; print LOG time(), "|"; print LOG "$spam_total|$spam_total_size|$spam_killed|$spam_killed_size\n"; close LOG; } $dbh->disconnect; sub clean_spam { find (\&old_spam, $maildir); } sub old_spam { if ($File::Find::dir =~ m!/\.Spam/(new|cur)$!o) { my $stale_days = $default_stale_days; my $age = int(-M $_); my $size = -s _; ++$spam_total; $spam_total_size += $size; print "$File::Find::dir\n" if DEBUG; if ($File::Find::dir =~ m!/virtual/ # dir (.+) # domain /../ # 1st 2 chars of user (.+) # username /Maildir/ # The Maildir !x) { my ($domain, $user) = ($1, $2); my $acct = "$user\@$domain"; print "$acct\n" if DEBUG; if ($sth->execute($acct)) { if (my $res = $sth->fetchrow_hashref()) { $stale_days = $res->{value}; } } else { warn "Can't read settings for $acct: ".$sth->errstr."\n"; } $sth->finish; } print "User's stale_days = $stale_days\n" if DEBUG; if ($age >= $stale_days) { if ($dry_run) { print("unlink $File::Find::name\t", "($age > ", "$stale_days", ")\n"); } else { print("unlink $File::Find::name age = $age\n") if DEBUG; unlink $File::Find::name or warn "Can't unlink $File::Find::name: $!\n"; } ++$spam_killed; $spam_killed_size += $size; } } } sub help() { my $rc = (0xffff & system ('pod2usage', $0)) >> 8; if ($rc != 0) { system ('pod2text', $0); } } sub bytes_to_human { my $bytes = shift; my $format = shift || 'k'; my $kb = $bytes / 1024; my $mb = $kb / 1024; my $gb = $mb / 1024; if (lc $format eq 'b') { return $bytes; } elsif (lc $format eq 'k') { return $kb; } elsif (lc $format eq 'm') { return $mb; } elsif (lc $format eq 'g') { return $gb; } } __END__ =head1 NAME clean-spam.pl - Clean out old spam =head1 SYNOPSIS clean-spam.pl [-n] [-d days] maildir =head1 DESCRIPTION Clean out old messages from users' .Spam folders. =over 4 =item -n Dry run. Don't actually delete the files. Instead print what would have been done. =item -d days Number of days for a file to be considered old. =item maildir The location of the mail dirs. =back =head1 LICENSE Copyright (c) 2003 Randy Smith All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. =head1 AUTHOR Randy Smith =cut