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