Skip to content

Instantly share code, notes, and snippets.

@pklaus
Created May 9, 2011 08:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pklaus/962247 to your computer and use it in GitHub Desktop.
Save pklaus/962247 to your computer and use it in GitHub Desktop.
MBOX to Maildir conversion scripts
#! /usr/bin/perl
#
# 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.
#
# Originally published by Russell Nelson with modifications by Richard Welty.
# Found on <http://www.averillpark.net/Maildir/>.
# Referred to by Philipp Klaus on <http://blog.philippklaus.de/2010/02/convert-mbox-to-maildir/>
#
# put into the public domain by Russell Nelson <nelson@qmail.org>
# NO GUARANTEE AT ALL; support is available for a fee from the author.
#
# Modified from original script by Richard Welty <rwelty@averillpark.net>
# uses safecat and after conversion examines individual files for
# UW-IMAP Status: flags, renaming files and moving them as appropriate
# to preserve Read/Unread and Answered status
while(($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;
}
$st_uid = (stat($dir))[4];;
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");
if (!-e "/var/spool/mail/$name") {
print "warning: ${name}'s mail box, /var/spool/mail/$name, doesn't exist (passwd: $passwd), skipping.\n";
next;
}
$st_size = (stat("/var/spool/mail/$name"))[7];;
if ($st_size == 0) {
print "warning: $name is zero length, skipping.\n";
next;
}
system "formail -I'From ' -s maildir . </var/spool/mail/$name";
chdir( "new");
system "maildir-script.sh $uid $gid";
# open(SPOOL, "</var/spool/mail/$name") || next;
# $i = time;
# while(<SPOOL>) {
# if (/^From /) {
# $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);
}
endpwent();
#!/bin/sh
#
# Originally published by Averill Park Networking on <http://www.averillpark.net/Maildir/>
# Referred to by Philipp Klaus on <http://blog.philippklaus.de/2010/02/convert-mbox-to-maildir/>
#
# Copyright 2003
# Richard Welty <rwelty@averillpark.net> ("The Author").
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The Author's name may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' BY THE AUTHOR AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#------------------------------------------------------------------
#
# excecute this script in Maildir/new for each
# Maildir where you wish to preserve Read/Unread and
# Answered flags. Note that X-Status: A may be specific
# to the mahogany MUA
#
# parameters are the numeric or text user and group that
# should own the file
#
for i in *
do
chown $1 $i
chgrp $2 $i
if grep -q "Status: RO" $i
then
# read
if grep -q "X-Status: A" $i
then
# seen & replied to
mv $i ../cur/$i:2,S,R
else
# just seen
mv $i ../cur/$i:2,S
fi
else
if grep -q "Status: O" $i
then
# old?
mv $i ../cur/$i:2,
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment