Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 15 Sep 2000 11:03:41 -0700
From:      Alan Batie <alan@rdrop.com>
To:        Matt Goward <mgoward@eviloverlord.org>
Cc:        freebsd-isp@FreeBSD.ORG
Subject:   Re: Mail box trimming tool
Message-ID:  <20000915110341.57155@rdrop.com>
In-Reply-To: <200009151707.NAA32297@eviloverlord.org>; from Matt Goward on Fri, Sep 15, 2000 at 01:07:08PM -0400
References:  <200009151707.NAA32297@eviloverlord.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--dobhs11T7y2rNNeA
Content-Type: text/plain; charset=us-ascii

On Fri, Sep 15, 2000 at 01:07:08PM -0400, Matt Goward wrote:
> Does anyone know of a tool that given a username and quota, will trim a 
> mail box by deleting the oldest message until it is at the quota?  

It doesn't compare the quota, but this is what I use to age my mailboxes:

#!/usr/local/bin/perl
eval "exec /usr/contrib/bin/perl -S $0 $*"
    if $running_under_some_shell;

$| = 1;

if ($#ARGV != 2) {
    print "Usage: $0 folder_dir archive_dir days_to_keep\n";
    exit(1);
}

#  If archive_dir == /dev/null, toss messages
$toss = 0;
$folder_dir = $ARGV[0];
$archive_dir = $ARGV[1];
if ($archive_dir eq "/dev/null") {
    $toss = 1;
}

$keep_days = $ARGV[2];
if ($keep_days =~ /[^0-9]/) {
    print STDERR "Number of days parameter must be numeric.\n";
    exit(1);
}
$keep_time = $keep_days * 24 * 3600;
$cur_time = time;

unless (opendir(FOLDERS, $folder_dir)) {
    print STDERR "Can't open '$folder_dir': $!\n";
    exit(1);
}

print;

while ($mbox = readdir(FOLDERS)) {
    $mailbox = $folder_dir . "/" . $mbox;
    if ( ! -f $mailbox ) {
	next;
    }

    print "\r$mbox...           ";
    $archive = $archive_dir . "/" . $mbox;
    $newbox = $folder_dir . "/$$." . $mbox;

    ($start_dev, $start_ino, $start_mode, $start_nlink, $start_uid, $start_gid,
     $start_rdev, $start_size, $start_atime, $start_mtime, $start_ctime,
     $start_blksize, $start_blocks) = stat($mailbox);

    unless (open(mailbox, "<$mailbox")) {
        print STDERR "Can't read $mailbox: $!\n";
	exit(1);
    }
    unless (open(newbox, ">$newbox")) {
        print STDERR "Can't write to $newbox: $!\n";
	exit(1);
    }
    if (!$toss) {
	unless (open(archive, ">>$archive")) {
	    print STDERR "Can't append to $archive: $!\n";
	    exit(1);
	}
    }

    #  In case there's junk in front of the first From...
    $arch_it = 1;
    while (<mailbox>) {
	if (/^From /) {
	    $msg_time = do parse_date($_);
	    if ($cur_time - $msg_time < $keep_time) {
		$arch_it = 0;
	    } else {
		$arch_it = 1;
	    }
	}
	if ($arch_it == 0) {
	    print newbox $_ || die "Write to $newbox failed: $!\n";
	} else {
	    if (!$toss) {
		print archive $_ || die "Write to $archive failed: $!\n";
	    }
	}
    }

    close(mailbox);
    close(newbox);
    if (!$toss) {
	close(archive);
    }

    ($stop_dev, $stop_ino, $stop_mode, $stop_nlink, $stop_uid, $stop_gid,
     $stop_rdev, $stop_size, $stop_atime, $stop_mtime, $stop_ctime,
     $stop_blksize, $stop_blocks) = stat($mailbox);

    if ($stop_mtime > $start_mtime) {
	print STDERR "$mailbox was modified after start of archive ---\n";
	print STDERR "Updated version left in $newbox.\n";
	exit(1);
    }

    unless (unlink($mailbox)) {
	print STDERR "Unable to unlink $mailbox: $!\n";
	exit(1);
    }

    if (!$toss) {
	if ( -z $archive ) {
	    unless (unlink($archive)) {
		print STDERR "Can't unlink empty archive '$archive': $!\n";
		exit(1);
	    }
	}
    }

    if ( -s $newbox ) {
	unless (link($newbox, $mailbox)) {
	    print STDERR "Unable to link $newbox to $mailbox: $!\n";
	    exit(1);
	}
    }

    unlink($newbox);
    # print "done\n";
}

print "\n";
exit(0);

#  Process From_ lines to get the number of days since Jan. 1, 1970:
#
#  From aahz!batie Fri Aug  3 08:30:41 1990

sub parse_date {
    local($from_space) = $_;
    
    $month_tbl{"Jan"} = 0;
    $month_tbl{"Feb"} = 1;
    $month_tbl{"Mar"} = 2;
    $month_tbl{"Apr"} = 3;
    $month_tbl{"May"} = 4;
    $month_tbl{"Jun"} = 5;
    $month_tbl{"Jul"} = 6;
    $month_tbl{"Aug"} = 7;
    $month_tbl{"Sep"} = 8;
    $month_tbl{"Oct"} = 9;
    $month_tbl{"Nov"} = 10;
    $month_tbl{"Dec"} = 11;

    $days_so_far{"Jan"} = 0;
    $days_so_far{"Feb"} = 31;
    $days_so_far{"Mar"} = 59;
    $days_so_far{"Apr"} = 90;
    $days_so_far{"May"} = 120;
    $days_so_far{"Jun"} = 151;
    $days_so_far{"Jul"} = 181;
    $days_so_far{"Aug"} = 212;
    $days_so_far{"Sep"} = 243;
    $days_so_far{"Oct"} = 273;
    $days_so_far{"Nov"} = 304;
    $days_so_far{"Dec"} = 334;

    $sec_per_year = 31536000;
    $sec_per_day = 86400;
    $sec_per_hour = 3600;
    $sec_per_minute = 60;
    $timezone = 8;

    ($d1, $d2, $d3, $month, $day, $time, $year) = split(/ +/, $from_space);
    ($hours, $minutes, $seconds) = split(/:/, $time);
    $month_num = $month_tbl{$month};

    #  determine the number of seconds since the beginning of the universe
    $clock = ($year - 1970) * $sec_per_year;
    if ($month ne "Jan") {
	$clock = $clock + $days_so_far{$month} * $sec_per_day;
    }
    $clock = $clock + ($day - 1) * $sec_per_day;
    $clock = $clock + $hours * $sec_per_hour;
    $clock = $clock + $minutes * $sec_per_minute;
    $clock = $clock + $seconds;

    #  calculate in the leap year fudge factors
    if ($year gt 1971) {
	$fudge_days = 0;
	$fudge_days = int(($year - 1971) / 4);

	if (($year % 4) == 0 && ($month_num > 1)) {
	    $fudge_days++;
	}
	$clock = $clock + $fudge_days * $sec_per_day;
    }

    #  calculate in the time shift westward from Greenwich
    $clock = $clock + $timezone * $sec_per_hour;

    #  worry about daylight savings time
    ($d1, $d2, $d3, $d4, $d5, $d6, $d7, $d8, $isdst) = localtime(time);
    if ($isdst != 0) {
	$clock = $clock - $sec_per_hour;
    }
    return $clock;
}

-- 
Alan Batie                   ______    www.rdrop.com/users/alan      Me
alan@batie.org               \    /    www.qrd.org         The Triangle
PGPFP DE 3C 29 17 C0 49 7A    \  /     www.pgpi.com   The Weird Numbers
27 40 A5 3C 37 4A DA 52 B9     \/      www.anti-spam.net       NO SPAM!

--dobhs11T7y2rNNeA
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: 2.6.2

iQCVAwUBOcJkfIv4wNua7QglAQGdfAP+OwQpzP0eylAsUN+0NsBMIJJWvZwZkatj
wtLLlt3AcD9E/Bmfs2wfbqYj4TnWF9d48MC+RwDveRGkb26A2m5e8W4Wqm72Ma2U
O742GZsHh1I24rHnvLf5CC99KnFhCwZvDt2nC7ZN+kUSa03A6Zjz4CSX8T8n5kn3
FfAGQnjs/cM=
=ZBo0
-----END PGP SIGNATURE-----

--dobhs11T7y2rNNeA--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-isp" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20000915110341.57155>