#! /usr/bin/perl # put into the public domain by Bruce Guenter # based heavily on code by Russell Nelson , also in # the public domain # NO GUARANTEE AT ALL # # Creates a maildir from a mbox file # Assumes that nothing is trying to modify the mailboxe # version 0.00 - first release to the public. # Modified by Matthew Lavy to "touch" each new # message with a time stamp based on the "Delivery-date:" header. # This modification, just like the original code, comes with NO GUARANTEES # at all. It depends on a working version of GNU touch (or an equivalent # touch which takes textual date stamps with a -d option. # 17/10/00 $TOUCH_CMD='/usr/bin/touch'; require 'stat.pl'; sub error { print STDERR join("\n", @_), "\n"; exit(1); } sub usage { print STDERR "usage: mbox2maildir [ ]\n"; exit(@_); } &usage(1) if $#ARGV != 1 && $#ARGV != 3;; $mbox = $ARGV[0]; $mdir = $ARGV[1]; $uid = $ARGV[2]; $gid = $ARGV[3]; &error("Cannot find $TOUCH_CMD.\nPlease install GNU touch, or change the top line of the script") unless -x $TOUCH_CMD; &error("can't open mbox '$mbox'") unless open(SPOOL, $mbox); -d $mdir || mkdir $mdir,0700 || &error("maildir '$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; $first_time = 1; sub open_msg { my $saved_fn = $fn; 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); unless ($first_time){ system("$TOUCH_CMD -d \"$saved_deliv_time\" $saved_fn"); } $first_time = 0; 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() { if(/^From /) { $saved_deliv_time = $deliv_time; open_msg($flags, \@header) if $in_header; undef $flags; undef @header; $in_header = 1; push @header, "MBOX-Line: $_"; } elsif($in_header) { #grab timestamp and set $deliv_time if (my $line = /^Delivery.+/){ $deliv_time = $_; $deliv_time =~ s/^Delivery-date:(.+)/$1/; push @header, $_; } elsif(/^\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);