nsbin/nsmail2maildir-01

102 lines
2.6 KiB
Perl

#!/usr/bin/perl
#
# Nico Schottelius <nico@schottelius.(net|org)>
# Date: 22-Sep-2003
# Last Modified: 26-Sep-2003
#
# some code stolen from mbox2maildir by Bruce Guenter <bruceg@em.ca>,
# which based on mbox2maildir by Russell Nelson <nelson@qmail.org>
# first this was a shellscript, now it's a perlscript
require 'stat.pl';
use File::Find;
# this is where all the mails goto
MAILDIR_BASE=/home/user/nico/Maildir/nsmail
for spool in $@; do
i=0
MAILDIR=$MAILDIR_BASE/`basename $spool`-$i;
MAIL=$spool
mkdir -p $MAILDIR/{new,cur,tmp}
if [ ! -d "$MAIL" -a -f "$MAIL" ]; then
echo Settings: $MAILDIR - $MAIL
echo mbox2maildir-test "$MAIL" "$MAILDIR"
read -p "converted $spool"
fi
$i++;
done
sub error {
print STDERR join("\n", @_), "\n";
exit(1);
}
sub usage {
print STDERR "usage: nsmail2maildir <nsmaildirfile> <destinationdir>\n";
exit(@_);
}
# check args
&usage(1) if $#ARGV != 2;
$mbox = $ARGV[0];
$ddir = $ARGV[1];
&error("can't open mbox '$mbox'") unless
open(SPOOL, $mbox);
-d $ddir || mkdir $ddir,0700 ||
&error("destinationdir '$mdir' doesn't exist and can't be created.");
-d $ddir || mkdir $ddir,0700 ||
&error("destinationdir '$mdir' doesn't exist and can't be created.");
chown($uid,$gid,$mdir) if defined($uid) && defined($gid);
chdir($mdir) || &error("fatal: unable to chdir to $mdir.");
-d "tmp" || mkdir("tmp",0700) || &error("unable to make tmp/ subdir");
-d "new" || mkdir("new",0700) || &error("unable to make new/ subdir");
-d "cur" || mkdir("cur",0700) || &error("unable to make cur/ subdir");
chown($uid,$gid,"tmp","new","cur") if defined($uid) && defined($gid);
$stamp = time;
sub open_msg {
my($flags,$header) = @_;
if($flags) {
if($flags =~ /RO/) { $fn = "cur/$stamp.$$.mbox:2,S"; }
elsif($flags =~ /O/) { $fn = "cur/$stamp.$$.mbox"; }
else { $fn = "new/$stamp.$$.mbox"; }
} else {
$fn = "new/$stamp.$$.mbox";
}
$stamp++;
close(OUT);
open(OUT, ">$fn") || &error("unable to create new message");
chown ($uid,$gid,$fn) if defined($uid) && defined($gid);
print OUT @$header, "\n";
}
$in_header = 0;
while(<SPOOL>) {
if(/^From /) {
open_msg($flags, \@header) if $in_header;
undef $flags;
undef @header;
$in_header = 1;
push @header, "MBOX-Line: $_";
} elsif($in_header) {
if(/^\s+$/o) {
$in_header = 0;
open_msg($flags, \@header);
} else {
$flags = $1 if /^Status:\s+(\S+)/oi;
push @header, $_;
}
} else {
s/^>From /From /;
print OUT || &error("unable to write to new message");
}
}
close(SPOOL);
open_msg($flags, \@header) if $in_header;
close(OUT);