d0d307
#!/usr/bin/perl
d0d307
    eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
d0d307
        if $running_under_some_shell;
d0d307
#
d0d307
# Usage:	bsd2cyrus inputfile
d0d307
#
d0d307
# Purpose:	Maps a user's Berkeley-format mail folder names into the
d0d307
#		Cyrus namespace
d0d307
#
d0d307
# Input:	List of usernames, one per line
d0d307
#
d0d307
# Output:	Lines of the format
d0d307
#		username:Cyrus-mailbox-name:BSD-mailbox-name
d0d307
#
d0d307
#$Id: bsd2cyrus,v 1.1 2004/02/04 12:59:42 karsten Exp $
d0d307
d0d307
require "find.pl";
d0d307
d0d307
# User's subdirectory where personal mail folders are stored 
d0d307
# (typically $HOME/mail)
d0d307
$maildir = "mail";
d0d307
d0d307
$inputfile  = "$ARGV[0]";
d0d307
if (! $inputfile) { die "Usage: $0 inputfile\n"; }
d0d307
d0d307
open (DATA, $inputfile) || die "can't open $inputfile";
d0d307
while (<data>) {
d0d307
    chop;
d0d307
    ($user,$pw,$uid,$gid,$quota,$cmnt,$gcos,$home) = getpwnam $_;
d0d307
    next if $home eq "";
d0d307
    &find("$home/$maildir");
d0d307
}
d0d307
close DATA;
d0d307
d0d307
foreach (@folders) {
d0d307
d0d307
    ($user,$folder) = split(/:/,$_,2);
d0d307
    if (! rfc822($folder) ) { next; }
d0d307
    @tokens = split(/\//, $folder);
d0d307
    $mailbox = $tokens[$#tokens]; 
d0d307
d0d307
    # Sanity checks - earlier tests should have caught these.
d0d307
d0d307
    next if ($mailbox =~ /\.gz$/);      # Skip gzipped files
d0d307
    next if ($mailbox =~ /\.Z$/);       # Skip compressed files
d0d307
    next if ($mailbox =~ /^\./);        # Skip hidden files
d0d307
d0d307
    # Replace "bad" characters with an underscore followed by 
d0d307
    # the ASCII representation of the "bad" character.
d0d307
d0d307
    $mailbox = rm_badchars($mailbox);
d0d307
    print "$user:user.$user.$mailbox:$folder\n";
d0d307
}
d0d307
d0d307
sub wanted {
d0d307
    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
d0d307
    -f _;
d0d307
    if ($_ ne '.') { push @folders, "$user:$dir/$_"; }
d0d307
}
d0d307
d0d307
sub rfc822 {
d0d307
d0d307
    my ($file) = @_;
d0d307
    my ($rc) = 1;
d0d307
    if (-d $file || -z $file || -B $file || -x $file) {
d0d307
        $rc = 0;
d0d307
    }
d0d307
    return $rc;
d0d307
}
d0d307
d0d307
sub rm_badchars {
d0d307
d0d307
    my ($mailbox) = @_;
d0d307
    $mailbox =~ s/ /_040/g;
d0d307
    $mailbox =~ s/\!/_041/g;
d0d307
    $mailbox =~ s/\"/_042/g;
d0d307
    $mailbox =~ s/\#/_043/g;
d0d307
d0d307
    return $mailbox;
d0d307
}
d0d307