#!/usr/bin/perl -w # # mbox2vpopmail # John Simpson 2005-07-08 # # Converts an "mbox" file to a Maildir as used by vpopmail # # Usage: # mbox2vpopmail userid@domain.xyz < mboxfile # # The vpopmail mailbox should already exist. This script will read the # messages from mboxfile and safely write them as individual message files # in the Maildir within the vpopmail mailbox. # ############################################################################### # # Copyright (C) 2005 John Simpson. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License, version 2, as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # or visit http://www.gnu.org/licenses/gpl.txt # ############################################################################### require 5.003 ; use strict ; my $vpopmail_user = "vpopmail" ; my ( @pw , $vpuid , $vpgid , $vpdir , $email , $user , $uhome , $maildir , $count , $ignored , $tf , $nf ) ; ############################################################################### ############################################################################### ############################################################################### # # figure out what we're doing @pw = getpwnam ( $vpopmail_user ) or die < ) ; close I ; $maildir = "$uhome/Maildir" ; print "Reading messages for $user\nWriting messages in $maildir\n" ; ############################################################################### # # convert the messages $count = $ignored = 0 ; $tf = $nf = "" ; while ( my $line = <> ) { if ( $line =~ /^From / ) { if ( $tf ) { close O ; chown ( $vpuid , $vpgid , $tf ) ; chmod ( 0600 , $tf ) ; rename ( $tf , $nf ) ; } $count ++ ; $tf = "$maildir/tmp/mbox2vpopmail.$$.$count" ; $nf = "$maildir/new/mbox2vpopmail.$$.$count" ; open ( O , ">$tf" ) or die "ERROR: can\'t create $tf: $!\n" ; } if ( $tf ) { $line =~ s/^>From /From / ; print O $line ; } else { $ignored ++ ; } } if ( $tf ) { close O ; chown ( $vpuid , $vpgid , $tf ) ; chmod ( 0600 , $tf ) ; rename ( $tf , $nf ) ; } ############################################################################### # # tell the user how it went print "Created $count messages in $maildir\n" ; if ( $ignored ) { print "Ignored $ignored lines of garbage at the beginning of input\n" ; } exit 0 ;