#!/usr/bin/perl
#-----------------------------------------------------------------------------
#
#  POPular -- A POP3 server and proxy for large mail systems
#
#  $Id: movedel,v 1.3 2001/02/09 15:59:07 sqrt Exp $
#
#  http://www.remote.org/jochen/mail/popular/
#
#-----------------------------------------------------------------------------
#
#  movedel
#
#  Moves all deleted mailboxes away. They get a new name, which consists
#  of the old name and an appended timestamp.
#
#-----------------------------------------------------------------------------
#
#  Copyright (C) 1999-2001  Jochen Topf <jochen@remote.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
#
#-----------------------------------------------------------------------------

use strict;

my $POPDIR  = "/pop";
my $BOXLIST = "/etc/popular/mb";
my $LOGFILE = "/var/log/popular/movedel";

#-----------------------------------------------------------------------------

my $timestamp = timestamp();

chdir("$POPDIR") or die("Can't cd to $POPDIR: $!\n");

open(LOG, ">>$LOGFILE") or die("Can't open $LOGFILE: $!\n");

print LOG "-" x 78, "\n";

my %boxhash;
foreach my $d (0x0..0xf) {
  open(LIST, "$BOXLIST/$d") or die("Can't open $BOXLIST/%d: $!\n");
  while(<LIST>) {
    next if (/^$/);
    next if (/^#/);
    chomp;
    my ($box, $quota) = split(/:/, $_, 2);
    $boxhash{$box} = 1;
  }
  close LIST;
}

my @dirs;
foreach my $d (0x0..0xf) {
  foreach my $n (0x00..0xff) {
    push(@dirs, sprintf("%x/%02x", $d, $n));
  }
}

print "Check for old boxes\n";
foreach my $dir (@dirs) {
  opendir(DIR, $dir) or die("Can't opendir $dir\n");
  my @boxes = grep(!/^\./, readdir(DIR));
  closedir(DIR);

  my @mvboxes = grep(! ($boxhash{"$dir/$_"} || /_\d{8}\.\d{6}$/), @boxes);

  foreach my $box (@mvboxes) {
    rename("$dir/$box", "$dir/${box}_$timestamp") or print STDERR "Can't rename $dir/$box\n";
    print LOG ("$dir/${box}_$timestamp\n");
  }
}

exit 0;


#-----------------------------------------------------------------------------
sub timestamp {
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

  return sprintf("%04d%02d%02d.%02d%02d%02d", 1900+$year, $mon+1, $mday, $hour, $min, $sec);
}


#-- THE END ------------------------------------------------------------------


syntax highlighted by Code2HTML, v. 0.9.1