Mailing List Archive

Silly Question Time
Re: Silly Question Time [ In reply to ]
At 04:10 PM 3/18/97 +1000, Mark Lillywhite wrote:
>Hi
>
>This is a really stoooopid question.
>
>How do I convert existing Mbox-format email messages to Maildir format?
>
>Surely this has been done before?

Russ Nelson has a perl script called convert-and-create (I think) that does
this. A link can be found on www.qmail.org

For local reasons, I've actually modified that script a bit to name the file
after the UIDL that may have been generated by qpopper. That way pop clients
that leave mail on servers don't *suddenly* re-get all that old mail.

I've attached the modified script which is also driven from stdin rather
than /etc/passwd for greater flexibility. Beware though this is hacked and
only to be used by people who have backed up their mbox files. It also
assumes uncorrupted mbox files as the filename is derived from the contents
of the mbox. Risky stuff.


Regards.


---------------------
#! /usr/local//bin/perl
# put into the public domain by Russell Nelson <nelson@qmail.org>
# Modified by Mark Delany <markd@mira.net.au>
# NO GUARANTEE AT ALL
#
# Creates maildirs for everyone in /etc/passwd who receives mail.
# Copies all their mail in /var/spool/mail into their maildir.
# Assumes that nothing is trying to modify the mailboxes in /var/spool/mail
# This assumption could be removed by locking the mailboxes and deleting
# the mail after moving it.
# version 0.00 - first release to the public.

require 'stat.pl';

while(<>) {
chomp;
($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
getpwent($_) {
if (!-e $dir) {
print "warning: ${name}'s home dir, $dir, doesn't exist (passwd:
$passwd), skipping.\n";
next;
}
&Stat($dir);
if ($uid != $st_uid) {
print "warning: $name is $uid, but $dir is owned by $st_uid,
skipping.\n";
next;
}
print "$name\n";
$spoolname = "$dir/Maildir";
-d $spoolname || mkdir $spoolname,0700 || die "fatal: mailbox doesn't
exist and can't be created.\n";
chown ($uid,$gid,$spoolname);
chdir($spoolname) || die("fatal: unable to chdir to $spoolname.\n");
-d "tmp" || mkdir("tmp",0700) || die("fatal: unable to make tmp/
subdir\n");
-d "new" || mkdir("new",0700) || die("fatal: unable to make new/
subdir\n");
-d "cur" || mkdir("cur",0700) || die("fatal: unable to make cur/
subdir\n");
chown ($uid,$gid,"tmp","new","cur");

delete $uidl;
open(SPOOL, "</var/mail/$name") || next;
$i = time;
while(<SPOOL>) {
if (/^X-UIDL: /) {
($j, $uidl) = split;
}
if (/^From /) {
close(OUT) if $fn;
link($fn, "new/$uidl") if $uidl;
delete $uidl;
$fn = sprintf("new/%d.$$.mbox", $i);
open(OUT, ">$fn") || die("fatal: unable to create new message");;
chown ($uid,$gid,$fn);
$i++;
next;
}
s/^>From /From /;
print OUT || die("fatal: unable to write to new message");
}
close(SPOOL);
close(OUT);
link($fn, "new/$uidl") if $uidl;
}

----------------------