On Sat, 2002-12-07 at 11:46, Joshua Schmidlkofer wrote:
> Yes please, that is exactly what I am looking for.  I will be migrating
> about 30 maildirs when this is all done. =)  By my 50,000 messages would
> be a good start =P

See the attached code.  I've made some modifications to it to make it a
bit more readable.  I can't really run it right now, so please let me
know if I broke it. :)

If you still get errors, change the die()s to warn()s and at least then
you'll have a list of the messages that won't import.  You should
probably use -w and strict as well.  I normally do, but I was in a rush
to crank out this code for testing last Sunday.

Cheers,

Mike
-- 
Mike Cathey - http://www.mikecathey.com/
Network Administrator
RTC Internet - http://www.catt.com/
#!/usr/bin/perl

use Mail::IMAPClient;

my $DEBUG = 0; # magic switch!

my $login = "<username>";
my $pass = "<pass>";
my $imaphost = "<imap_server>";
my $folder = "INBOX";
my $newspool = "<full path to new/ including trailing />";
my $curspool = "<full path to cur/ including trailing />";
my $msgid = undef;	# holds msg id after it's appended
					# 	and before we push it onto @seen if it's from cur/
my @new;			# will hold filenames of new messages in the maildir
my @current;		# will hold filenames of old messages in the maildir
my @seen;			# msgid's in $cur are pushed here so we can mark them \Seen

# get the filenames from cur/
opendir(DIR, $curspool) || die "can't opendir $some_dir: $!";
@current = grep { !/^\./ && -f "$curspool/$_" } readdir(DIR);
closedir DIR;

# get the filenames from new/
opendir(DIR, $newspool) || die "can't opendir $some_dir: $!";
@new = grep { !/^\./ && -f "$newspool/$_" } readdir(DIR);
closedir DIR;

if ($DEBUG == 1) {
	my $old = scalar(@current);
	my $new = scalar(@new);
	print "$old old messages...\n";
	print "$new new messages...\n";
}

# connect to the imap serer
my $imap = Mail::IMAPClient->new(
        Server => $imaphost,
        User   => $login,
        Password=> $pass
) or die "Can't connect to $imaphost: $@!";

$imap->select($folder);

# iterate through new/ message filenames appending them to the imap folder
foreach $msg (@new) {
        $filename = $newspool . $msg;

        $msgid = $imap->append_file(
                $folder,
                $filename 
        ) or die "Could not append_file: $@\n";

        if  ( $msgid ) {
                print "Message inserted ok == $msgid\n" if $DEBUG == 1;
        } else { # you should never get here because of the die() above
                print "Message insertion error (new/$msg)!\n";
        }
}

# iterate through cur/ message filenames appending them to the imap folder,
#	pushing their msgids onto @seen so that we can mark them read later
foreach $msg (@current) {
        $filename = $curspool . $msg;

        $msgid = $imap->append_file(
                $folder,
                $filename 
        ) or die "Could not append_file: $@\n";

        if  ( $msgid ) {
                print "Message inserted ok == $msgid\n" if $DEBUG == 1;
                push (@seen, $msgid);
        } else { # you should never get here because of the die() above
                print "Message insertion error (cur/$msg)!\n";
        }
}

# now mark messages seen/unseen
print "now marking old messages seen...\n" if $DEBUG == 1;;
$imap->set_flag("\Seen", @seen);

# logout and close the connection
$imap->logout or die "Could not logout: $@\n";

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to