#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
use Mail::Mbox::MessageParser;

my $usage = 
"ARGS must be :
\targv1 : mbox file
\targv2 : imap host
\targv3 : imap user (password will be prompted)
\targv4 : destination mailbox on imap server\n";

die($usage) if(@ARGV != 4);
my ($file,$host,$user,$dest) = @ARGV;

my $password = '';
print "Please enter password:\n";
while((my $c = getc) ne "\n"){ $password .= $c; }

my $fileh = new FileHandle($file) or die "Unable to open mailbox file\n";
my $mbox  = new Mail::Mbox::MessageParser( { 'file_name' => $file , 'file_handle' => $fileh , 'enable_cache' => 0} ) or die "Unable to create Message Parser object\n";
my $imap  = new Mail::IMAPClient( 'Server' => $host , 'User' => $user , 'Authmechanism' => 'CRAM-MD5', 'Password' => $password  ) or die "Unable to connect to imap server\n";
$mbox->prologue;
while(!$mbox->end_of_file()){
	my $email = $mbox->read_next_email();
	$$email =~ s/\n/\r\n/g;
	$imap->append_string($dest,$$email) or die "Unable to add message $@\n";
}
$imap->disconnect() or die "Unable to disconnect\n";

