2 # Copyright 2008-2002 Timo Sirainen
3 # Last updated: 2012-07-28
5 # NOTE: Requires Dovecot v2.0.13+ for POP3 'O' entries
7 # Based largely on courier-dovecot-migrate.pl v1.1.7:
8 # cpanel12 - maildir-migrate Copyright(c) 2008 cPanel, Inc.
10 # copyright@cpanel.net http://cpanel.net
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions are met:
14 # * Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # * Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 # * Neither the name of the cPanel, Inc. nor the
20 # names of its contributors may be used to endorse or promote products
21 # derived from this software without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY CPANEL, INC. "AS IS" AND ANY
24 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL CPANEL, INC BE LIABLE FOR ANY
27 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 # Key files in maildirs
39 my $courier_imap_uidfile = 'courierimapuiddb';
40 my $courier_pop3_uidfile = 'courierpop3dsizelist';
41 my $courier_subscriptions_file = 'courierimapsubscribed';
42 my $courier_keywords_dir = 'courierimapkeywords/';
43 my $courier_keywords_file = 'courierimapkeywords/:list';
44 my $dovecot_uidfile = 'dovecot-uidlist';
45 my $dovecot_uidvalidity_file = 'dovecot-uidvalidity';
46 my $dovecot_subscriptions_file = 'subscriptions';
47 my $dovecot_keywords_file = 'dovecot-keywords';
50 my $do_conversion = 0;
56 my $maildir_subdirs = -1;
57 my $global_error_count = 0;
58 my $global_mailbox_count = 0;
59 my $global_user_count = 0;
60 my $uidlist_write_count = 0;
61 my $convert_to_dovecot = 0;
62 my $convert_to_courier = 0;
64 my $maildir_name = 'Maildir';
68 'convert' => \$do_conversion,
70 'overwrite' => \$overwrite,
71 'recursive' => \$recursive,
72 'to-dovecot' => \$convert_to_dovecot,
73 'to-courier' => \$convert_to_courier,
77 Getopt::Long::GetOptions(%opts);
80 my $mailroot = shift @ARGV || '.';
83 if ( $convert_to_dovecot && $convert_to_courier ) {
84 print "It is not possible to convert to both Dovecot and Courier formats simultaneously!\n";
88 print $do_conversion ? 'Converting' : 'Testing conversion' unless $quiet;
90 if ($convert_to_courier) {
91 print " to Courier format\n" unless $quiet;
92 $conversion_type = 'courier';
94 elsif ($convert_to_dovecot) {
95 print " to Dovecot format\n" unless $quiet;
96 $conversion_type = 'dovecot';
99 print " based on newest timestamps\n" unless $quiet;
100 $conversion_type = 'auto';
104 # Check/Convert maildirs
105 print "Finding maildirs under $mailroot\n" if ( !$quiet );
106 if ( is_maildir($mailroot) ) {
107 check_maildir($mailroot);
109 elsif ( -d "$mailroot/$maildir_name" ) {
110 if ( !is_maildir("$mailroot/$maildir_name") ) {
111 print STDERR "$mailroot/$maildir_name doesn't seem to contain a valid Maildir\n";
114 check_maildir("$mailroot/$maildir_name");
118 if ( $depth > 0 || !userdir_check($mailroot) ) {
119 $depth-- if ( $depth > 0 );
120 if ( !depth_check( $mailroot, $depth ) ) {
121 print STDERR "No maildirs found\n";
129 print "\nTotal: $global_mailbox_count mailboxes / $global_user_count users\n";
130 print " $global_error_count errors\n";
132 if ( !$do_conversion ) {
133 print "No actual conversion done, use --convert parameter\n";
136 print "$uidlist_write_count $dovecot_uidfile files written\n";
138 print "\nWARNING: Badly done migration will cause your IMAP and/or POP3 clients to re-download all mails. Read http://wiki.dovecot.org/Migration carefully.\n";
142 my ( $dir, $map ) = @_;
144 my @scan_maildir_files;
145 if ( opendir my $scan_maildir_dh, $dir ) {
146 @scan_maildir_files = readdir($scan_maildir_dh);
147 closedir $scan_maildir_dh;
149 foreach my $real_filename (@scan_maildir_files) {
150 next if ( $real_filename eq "." || $real_filename eq ".." );
153 if ( $real_filename =~ /^([^:]+):2,/ ) {
157 $base_filename = $real_filename;
159 $$map{$base_filename} = $real_filename;
163 sub read_courier_pop3 {
166 my ( $pop3_uidv, $pop3_nextuid ) = ( -1, 0 );
167 my $filename_map = {};
170 my $pop3_fname = "$dir/$courier_pop3_uidfile";
171 open( $f, $pop3_fname ) || die $!;
173 if ( $pop3_hdr =~ /^\/2 (\d+) (\d+)$/ ) {
174 # /2 <next uid> <uidvalidity>
177 elsif ( $pop3_hdr =~ /^\/1 (\d+)$/ ) {
181 elsif ( $pop3_hdr !~ /^\// ) {
186 print STDERR "$pop3_fname: Broken header: $pop3_hdr\n";
188 return $filename_map;
196 my ( $full_fname, $fsize, $uid, $uidv );
198 if ( /^([^ ]+) (\d+) (\d+):(\d+)$/ ) {
200 ( $full_fname, $fsize, $uid, $uidv ) = ( $1, $2, $3, $4 );
202 elsif ( /^([^ ]+) (\d+) (\d+)$/ ) {
204 ( $full_fname, $fsize, $uid ) = ( $1, $2, $3 );
207 elsif ( /^([^ ]+) (\d+)$/ ) {
209 ( $full_fname, $fsize ) = ( $1, $2 );
214 $global_error_count++;
215 print STDERR "$pop3_fname: Broken line: $_\n";
220 my $fname = $full_fname;
221 $fname =~ s/^([^:]+).*$/$1/;
226 foreach (split(//, $fname)) {
227 if (ord($_) < 0x21 || ord($_) > 0x7e || $_ eq "'" || $_ eq '"' || $_ eq "+") {
228 $uidl .= sprintf("+%02X", ord($_));
235 $uidl = "UID$uid-$uidv";
240 $filename_map->{$fname} = [ 0, $uidl, $fsize, $full_fname, $line ];
243 return $filename_map;
246 sub read_courier_imap {
247 my ( $dir, $filename_map ) = @_;
249 # check if we can preserve IMAP UIDs
250 my $imap_fname = "$dir/$courier_imap_uidfile";
251 if ( !-f $imap_fname ) {
252 print "$imap_fname: OK\n" if ( !$quiet );
257 open( $f, $imap_fname ) || die $!;
259 if ( $imap_hdr !~ /^1 (\d+) (\d+)$/ ) {
260 $global_error_count++;
261 print STDERR "$imap_fname: Broken header: $imap_hdr\n";
265 my ( $uidv, $nextuid ) = ( $1, $2 );
268 my $found_files_looked_up = 0;
272 if (/^(\d+) (.*)$/) {
273 my ( $uid, $full_fname ) = ( $1, $2 );
275 # get the base filename
276 my $fname = $full_fname;
277 $fname =~ s/^([^:]+).*$/$1/;
279 if ( defined $filename_map->{$fname} ) {
280 $filename_map->{$fname}->[0] = $uid;
284 $filename_map->{$fname} = [ $uid, "", 0, $full_fname, 0 ];
286 $nextuid = $uid + 1 if ($uid >= $nextuid);
289 $global_error_count++;
290 print STDERR "$imap_fname: Broken header\n";
295 return ( $uidv, $nextuid, $filename_map );
298 sub write_dovecot_uidlist {
299 my ( $dir, $uidv, $nextuid, $owner_uid, $owner_gid, $filename_map ) = @_;
301 my $uidlist_fname = "$dir/$dovecot_uidfile";
302 if ( !$overwrite && -f $uidlist_fname ) {
303 print "$uidlist_fname already exists, not overwritten\n" if ( !$quiet );
306 return if (scalar keys %{$filename_map} == 0);
308 return if ( !$do_conversion );
311 # only pop3 UIDLs, generate a new uidvalidity
315 # POP3 clients may want to get POP3 UIDLs in the same order always.
316 # Preserve the order with O flag supported in dovecot 2.0.13+ and dont change order of IMAP UIDs.
318 foreach my $fname ( keys %{$filename_map} ) {
319 my $order = $filename_map->{$fname}->[4];
320 $filename_map->{$fname}->[5]=$order;
324 foreach my $order ( sort { $a <=> $b } keys %order_map ) {
325 my $file_ar = $filename_map->{ $order_map{$order} };
326 if ($file_ar->[0] == 0) {
327 $file_ar->[0] = $nextuid;
331 if ($file_ar->[0] <= $prev_uid) {
334 $prev_uid = $file_ar->[0];
338 # sort all messages by their imap UID
340 ($filename_map->{$a}[0] || 'inf') <=> ($filename_map->{$b}[0] || 'inf')
341 } keys %$filename_map;
345 foreach my $fname ( @all ) {
346 my $file_ar = $filename_map->{ $fname };
347 my $uid = $file_ar->[0];
348 if ($uid == 0 # only in pop3 list or marked for new UID
349 # OR: other ill-ordered UID message
350 || $uid <= $prev_uid)
352 # Assign a new IMAP UID:
357 $uidlist_map{$uid} = $fname;
360 open( my $dovecot_uidlist_fh, '>', $uidlist_fname ) || die $!;
361 print $dovecot_uidlist_fh "3 V$uidv N$nextuid\n";
362 foreach my $uid ( sort { $a <=> $b } keys %uidlist_map ) {
363 my $file_ar = $filename_map->{ $uidlist_map{$uid} };
364 print $dovecot_uidlist_fh "$uid ";
366 print $dovecot_uidlist_fh "O".$file_ar->[5]." ";
368 print $dovecot_uidlist_fh 'P' . $file_ar->[1] . ' ' if ( $file_ar->[1] ne "" );
369 print $dovecot_uidlist_fh 'W' . $file_ar->[2] . ' ' if ( $file_ar->[2] > 0 );
370 print $dovecot_uidlist_fh ':' . $file_ar->[3] . "\n";
372 close $dovecot_uidlist_fh;
373 chown $owner_uid, $owner_gid, $uidlist_fname;
374 $uidlist_write_count++;
377 sub convert_keywords {
378 my ( $dir, $owner_uid, $owner_gid ) = @_;
380 my $courier_mtime = ( stat("$dir/$courier_keywords_file") )[9] || 0;
381 my $dovecot_mtime = ( stat("$dir/$dovecot_keywords_file") )[9] || 0;
383 # No need to convert if there are no keywords files
384 return unless ( $courier_mtime || $dovecot_mtime );
386 # If we're doing auto-conversion, find the newest keywords file
387 my $convert_to = $conversion_type;
388 if ( $convert_to eq 'auto' ) {
389 $convert_to = $dovecot_mtime > $courier_mtime ? 'courier' : 'dovecot';
392 if ( $convert_to eq 'dovecot' ) {
393 # Courier to Dovecot keyword conversion
394 my $keyword_dir = "$dir/courierimapkeywords";
395 my $dovecot_keyfname = "$dir/dovecot-keywords";
397 if ( !-f "$keyword_dir/:list" ) {
403 if ( !$overwrite && -f $dovecot_keyfname ) {
404 print "$dovecot_keyfname already exists, not overwritten\n" if ( !$quiet );
408 my ( %keywords, %files );
410 open( $f, "$keyword_dir/:list" ) || die $!;
417 $keywords{$_} = scalar keys %keywords;
420 # read filenames -> keywords mapping
422 if (/([^:]+):([\d ]+)$/) {
424 foreach ( sort { $a <=> $b } split( " ", $2 ) ) {
425 $files{$fname} .= chr( 97 + $_ );
429 print STDERR "$keyword_dir/:list: Broken entry: $_\n";
434 # read updates from the directory
437 if ( opendir my $kw_dh, $keyword_dir ) {
438 @update_files = readdir($kw_dh);
441 foreach (@update_files) {
442 next if ( $_ eq ":list" || $_ eq "." || $_ eq ".." );
445 if (/^\.(\d+)\.(.*)$/) {
446 my ( $num, $base_fname ) = ( $1, $2 );
447 if ( !defined $updates{$fname} ) {
448 $updates{$fname} = $num;
451 my $old = $updates{$fname};
452 if ( $old >= 0 && $num > $old ) {
453 $updates{$fname} = $num;
459 # "fname" overrides .n.fnames
460 $updates{$fname} = -1;
465 foreach ( keys %updates ) {
467 my $num = $updates{$_};
471 $fname = $base_fname;
474 $fname = ".$num.$base_fname";
478 open( $f, "$keyword_dir/$fname" ) || next;
484 if ( defined $keywords{$kw} ) {
485 $idx = $keywords{$kw};
488 $idx = scalar keys %keywords;
489 $keywords{$kw} = $idx;
491 $kw_list[ scalar @kw_list ] = $idx;
496 foreach ( sort { $a <=> $b } @kw_list ) {
497 $files{$fname} .= chr( 97 + $_ );
501 return if ( !$do_conversion );
503 # write dovecot-keywords file
504 open( $f, ">$dovecot_keyfname" ) || die $!;
505 foreach ( sort { $keywords{$a} <=> $keywords{$b} } keys %keywords ) {
506 my $idx = $keywords{$_};
507 print $f "$idx $_\n";
510 chown $owner_uid, $owner_gid, $dovecot_keyfname;
512 # update the maildir files
513 my $cur_dir = "$dir/cur";
515 if ( opendir my $cur_dir_dh, $cur_dir ) {
516 @cur_files = readdir($cur_dir_dh);
517 closedir $cur_dir_dh;
519 foreach (@cur_files) {
520 my $fname = $cur_dir . '/' . $_;
522 my ( $base_fname, $flags, $extra_flags );
523 if (/^([^:]+):2,([^,]*)(,.*)?$/) {
524 ( $base_fname, $flags, $extra_flags ) = ( $1, $2, $3 );
525 $extra_flags = "" if ( !defined $extra_flags );
528 $base_fname = $fname;
533 if ( defined $files{$base_fname} ) {
535 # merge old and new flags
537 foreach ( sort split( "", $files{$base_fname} ) ) {
540 foreach ( sort split( "", $flags ) ) {
544 foreach ( sort keys %newflags ) {
547 my $new_fname = "$cur_dir/$base_fname:2,$flags$extra_flags";
548 if ( $fname ne $new_fname ) {
549 rename( $fname, $new_fname )
550 || print STDERR "rename($fname, $new_fname) failed: $!\n";
557 # Dovecot to Courier keywords conversion
558 return unless $dovecot_mtime;
560 if ( !$overwrite && -f "$dir/$courier_keywords_file" ) {
561 print "$courier_keywords_file already exists, not overwritten\n" if ( !$quiet );
565 # Read Dovecot keywords list into memory
566 open my $dovecot_kw_fh, '<', "$dir/$dovecot_keywords_file" || die $!;
568 while ( my $line = readline($dovecot_kw_fh) ) {
570 if ( $line =~ /(\d+)\s+(.+)/ ) {
572 # Number then Keyword
576 close $dovecot_kw_fh;
578 # Scan files in cur for keywords
579 my $cur_dir = "$dir/cur";
580 my %file_keyword_map;
583 if ( opendir my $cur_dir_dh, $cur_dir ) {
584 @cur_files = readdir($cur_dir_dh);
585 closedir $cur_dir_dh;
587 foreach my $basename (@cur_files) {
592 # Split out and process flags
593 if ( $basename =~ /^([^:]+):2,([^,]*)(,.*)?$/ ) {
594 ( $basename, $flags, $extra_flags ) = ( $1, $2, $3 );
595 $extra_flags = "" unless ( defined $extra_flags );
602 foreach my $key ( sort split( //, $flags ) ) {
603 my $val = ord($key) - 97;
604 next unless ( $val >= 0 && $val < 26 );
605 next unless ( defined $keywords{$val} );
606 $keywords .= ' ' . $val;
609 $keywords =~ s/^\s+//;
610 $file_keyword_map{$basename} = $keywords;
614 return unless ($do_conversion);
616 # Make courier keywords directory if necessary
617 my $key_dir = "$dir/$courier_keywords_dir";
618 unless ( -d $key_dir ) {
621 chown $owner_uid, $owner_gid, $key_dir;
624 # Remove any old courier keywords files
625 my @courier_keywords_files;
626 if ( opendir my $courier_keywords_dh, $key_dir ) {
627 @courier_keywords_files = readdir($courier_keywords_dh);
628 closedir $courier_keywords_dh;
630 foreach my $file (@courier_keywords_files) {
631 $file = $key_dir . $file;
632 next unless -f $file;
636 # Write courier keywords list
637 return unless ( scalar %keywords );
638 open my $courier_kw_fh, '>', "$dir/$courier_keywords_file" || die $!;
639 foreach my $num ( sort keys %keywords ) {
640 print $courier_kw_fh $keywords{$num} . "\n";
642 print $courier_kw_fh "\n";
643 foreach my $file ( sort keys %file_keyword_map ) {
644 print $courier_kw_fh $file . ':' . $file_keyword_map{$file} . "\n";
646 close $courier_kw_fh;
647 chown $owner_uid, $owner_gid, "$dir/$courier_keywords_file";
651 sub convert_subscriptions {
652 my ( $dir, $owner_uid, $owner_gid ) = @_;
654 my $courier_mtime = ( stat("$dir/$courier_subscriptions_file") )[9] || 0;
655 my $dovecot_mtime = ( stat("$dir/$dovecot_subscriptions_file") )[9] || 0;
657 # No need to convert if there is no subscriptions files
658 return unless ( $courier_mtime || $dovecot_mtime );
660 # If we're doing auto-conversion, find the newest subscription file
661 my $convert_to = $conversion_type;
662 if ( $convert_to eq 'auto' ) {
663 $convert_to = $dovecot_mtime > $courier_mtime ? 'courier' : 'dovecot';
666 my $src_file = "$dir/$dovecot_subscriptions_file";
667 my $dst_file = "$dir/$courier_subscriptions_file";
668 my $src_mtime = $dovecot_mtime;
669 my $dst_mtime = $courier_mtime;
670 if ( $convert_to eq 'dovecot' ) {
671 $src_file = "$dir/$courier_subscriptions_file";
672 $dst_file = "$dir/$dovecot_subscriptions_file";
673 $src_mtime = $courier_mtime;
674 $dst_mtime = $dovecot_mtime;
678 if ( $dst_mtime && !$overwrite ) {
679 print "$dst_file already exists, not overwritten\n" if ( !$quiet );
682 if ( $dst_mtime && !-f $dst_file ) {
683 print "$dst_file already exists as something other than a file\n" if ( !$quiet );
686 unless ($src_mtime) {
689 unless ( -f $src_file ) {
690 print "$src_file isn't a regular file\n" if ( !$quiet );
694 return unless ($do_conversion);
696 open( my $src_fh, '<', $src_file ) || die $!;
697 open( my $dst_fh, '>', $dst_file ) || die $!;
698 while ( my $line = readline($src_fh) ) {
700 if ( $line =~ /^INBOX$/i ) {
701 print $dst_fh "INBOX\n";
703 elsif ( $convert_to eq 'dovecot' ) {
704 if ( $line =~ /^INBOX\.(.*)$/i ) {
705 print $dst_fh "$1\n";
709 # Unknown. The dovecot migrate script leaves these as-is...
710 print $dst_fh "$line\n";
715 # converting to Courier INBOX namespace
716 if ( $line =~ /\S/ ) {
717 print $dst_fh "INBOX.$line\n";
723 chown $owner_uid, $owner_gid, $dst_file;
726 sub check_maildir_single {
727 my ( $dir, $childbox ) = @_;
734 # Store the relevant stats()
735 my @courier_pop_stat = ();
736 @courier_pop_stat = stat("$dir/$courier_pop3_uidfile") unless $childbox;
737 my @courier_imap_stat = stat("$dir/$courier_imap_uidfile");
738 my @dovecot_stat = stat("$dir/$dovecot_uidfile");
741 my $courier_pop_mtime = ( scalar @courier_pop_stat > 0 ) ? $courier_pop_stat[9] : 0;
742 my $courier_imap_mtime = ( scalar @courier_imap_stat > 0 ) ? $courier_imap_stat[9] : 0;
743 my $dovecot_mtime = ( scalar @dovecot_stat > 0 ) ? $dovecot_stat[9] : 0;
745 # Determine conversion type
746 my $convert_uidl_to = $conversion_type;
748 if ( $convert_uidl_to eq 'auto' ) {
749 $convert_uidl_to = $dovecot_mtime > $courier_pop_mtime && $dovecot_mtime > $courier_imap_mtime ? 'courier' : 'dovecot';
754 if ( $convert_uidl_to eq 'dovecot' ) {
757 unless ( $courier_pop_mtime || $courier_imap_mtime ) {
758 print "$dir: No imap/pop3 uidlist files\n" if ( !$quiet && !$childbox );
762 $owner_uid = $courier_pop_mtime ? $courier_pop_stat[4] : $courier_imap_stat[4];
763 $owner_gid = $courier_pop_mtime ? $courier_pop_stat[5] : $courier_imap_stat[5];
769 if ( $courier_pop_mtime) {
770 $filename_map = read_courier_pop3( $dir );
773 if ($courier_imap_mtime) {
774 ( $uidv, $nextuid, $filename_map ) = read_courier_imap( $dir, $filename_map );
776 $global_mailbox_count++;
777 write_dovecot_uidlist( $dir, $uidv, $nextuid, $owner_uid, $owner_gid, $filename_map );
778 remove_dovecot_caches($dir) if ($overwrite);
783 unless ($dovecot_mtime) {
784 print "$dir: No imap/pop3 uidlist files\n" if ( !$quiet && !$childbox );
788 $owner_uid = $dovecot_stat[4];
789 $owner_gid = $dovecot_stat[5];
790 my ( $uidv, $nextuid, $msguids ) = read_dovecot_uidfile($dir);
792 write_courier_pop3( $dir, $uidv, $nextuid, $owner_uid, $owner_gid, $msguids );
793 write_courier_imap( $dir, $uidv, $nextuid, $owner_uid, $owner_gid, $msguids );
797 # If we get here we did a UIDL conversion. Now convert subscriptions and keywords
799 convert_subscriptions( $dir, $owner_uid, $owner_gid );
800 convert_keywords( $dir, $owner_uid, $owner_gid );
803 sub remove_dovecot_caches {
805 foreach my $file ( qw(dovecot.index dovecot.index.cache dovecot.index.log dovecot.index.log2), $dovecot_uidvalidity_file ) {
806 unlink $dir . '/' . $file;
808 unlink glob( $dir . '/' . $dovecot_uidvalidity_file . '.*' );
811 sub read_dovecot_uidfile {
813 my $dovecot_uidfile = "$dir/$dovecot_uidfile";
817 my $dovecot_uid_version;
820 if ( !-f $dovecot_uidfile ) {
821 print "$dovecot_uidfile: OK\n" if ( !$quiet );
826 open( $dovecot_uid_fh, '<', $dovecot_uidfile ) || die $!;
827 my $dovecot_hdr = readline($dovecot_uid_fh);
828 if ( $dovecot_hdr =~ /^3\s+(.+)$/ ) {
830 $dovecot_uid_version = 3;
831 foreach my $part ( split( /\s+/, $options ) ) {
832 if ( $part =~ /(\w)(.+)/ ) {
835 if ( $type eq 'V' ) {
838 elsif ( $type eq 'N' ) {
845 $global_error_count++;
846 print STDERR "$dovecot_uidfile: Broken header: $dovecot_hdr\n";
847 close $dovecot_uid_fh;
851 elsif ( $dovecot_hdr =~ /^1\s+(\S+)\s+(\S+)$/ ) {
852 $dovecot_uid_version = 1;
857 $global_error_count++;
858 print STDERR "$dovecot_uidfile: Broken header: $dovecot_hdr\n";
859 close $dovecot_uid_fh;
863 while ( my $line = readline($dovecot_uid_fh) ) {
866 my @prts = split( /\s+/, $line );
868 if ( $dovecot_uid_version eq '3' ) {
869 next unless ( scalar @prts >= 2 );
870 my $msgnum = shift @prts;
871 my $filename = pop @prts;
872 $filename =~ s/^\://;
875 # Dovecot may or may not store the sizes for each message in the uidl file
876 # S# is the size with UNIX newlines, W# is the size with windows newlines
878 foreach my $subprt (@prts) {
879 if ( $subprt =~ s/^W// ) {
884 push @msguids, [ $msgnum, $filename, $msgsize ];
885 $nextuid = $msgnum + 1 if ( $msgnum >= $nextuid );
890 next unless ( scalar @prts == 2 );
891 push @msguids, [ $prts[0], $prts[1], undef ];
892 $nextuid = $prts[0] + 1 if ( $prts[0] >= $nextuid );
896 return ( $uidv, $nextuid, \@msguids );
899 sub write_courier_pop3 {
903 my $owner_uid = shift;
904 my $owner_gid = shift;
907 # Check file/overwrite/conversion
908 my $uidlist_fname = "$dir/$courier_pop3_uidfile";
909 if ( !$overwrite && -f $uidlist_fname ) {
910 print "$uidlist_fname already exists, not overwritten\n" if ( !$quiet );
914 return if ( !$do_conversion );
916 # Check that all entries have sizes.. We don't write the pop3 file if we can't do so correctly
917 foreach my $msg_ar ( @{$msguids} ) {
918 unless ( defined $msg_ar->[2] ) {
919 print "Missing some sizes for $uidlist_fname, skipping\n" if ( !$quiet );
925 open( my $courier_pop_fh, '>', $uidlist_fname ) || die $!;
926 print $courier_pop_fh "/2 $nextuid $uidv\n";
927 foreach my $msg_ar ( @{$msguids} ) {
928 print $courier_pop_fh $msg_ar->[1] . ' ' . $msg_ar->[2] . ' ' . $msg_ar->[0] . ':' . $uidv . "\n";
930 close $courier_pop_fh;
931 chown $owner_uid, $owner_gid, $uidlist_fname;
932 $uidlist_write_count++;
936 sub write_courier_imap {
940 my $owner_uid = shift;
941 my $owner_gid = shift;
944 # Check file/overwrite/conversion
945 my $uidlist_fname = "$dir/$courier_imap_uidfile";
946 if ( !$overwrite && -f $uidlist_fname ) {
947 print "$uidlist_fname already exists, not overwritten\n" if ( !$quiet );
951 return if ( !$do_conversion );
954 open( my $courier_imap_fh, '>', $uidlist_fname ) || die $!;
955 print $courier_imap_fh "1 $uidv $nextuid\n";
956 foreach my $msg_ar ( @{$msguids} ) {
957 my $filename = $msg_ar->[1];
958 $filename =~ s/\:2.*$//;
959 print $courier_imap_fh $msg_ar->[0] . ' ' . $filename . "\n";
961 close $courier_imap_fh;
962 chown $owner_uid, $owner_gid, $uidlist_fname;
963 $uidlist_write_count++;
970 my $orig_mailboxes = $global_mailbox_count;
972 check_maildir_single( $dir, 0 );
973 my @check_maildir_files;
974 if ( opendir my $check_maildir_dh, $dir ) {
975 @check_maildir_files = readdir($check_maildir_dh);
976 closedir $check_maildir_dh;
978 foreach my $file (@check_maildir_files) {
979 next unless ( $file =~ /^\./ );
980 next if ( $file =~ /^\.?\.$/ );
981 $file = $dir . '/' . $file;
982 next if ( -l $file );
983 check_maildir_single( $file, 1 );
986 $global_user_count++ if ( $orig_mailboxes != $global_mailbox_count );
992 # Do we need to check for the courier specific files here or is it enough to assume every maildir will have a cur directory?
993 return ( -f "$dir/$courier_pop3_uidfile" || -f "$dir/$courier_imap_uidfile" || -d "$dir/cur" );
1000 my @userdir_check_files;
1001 if ( opendir my $userdir_dh, $dir ) {
1002 @userdir_check_files = readdir($userdir_dh);
1003 closedir $userdir_dh;
1005 foreach my $userdir (@userdir_check_files) {
1006 $userdir = $dir . '/' . $userdir;
1007 next if ( -l $userdir );
1008 next if ( !-d $userdir );
1010 if ( $maildir_subdirs == -1 ) {
1012 # unknown if we want $maildir_name/ or not
1013 if ( -d "$userdir/$maildir_name" && is_maildir("$userdir/$maildir_name") ) {
1014 $maildir_subdirs = 1;
1016 elsif ( is_maildir($userdir) ) {
1017 $maildir_subdirs = 0;
1024 if ( $maildir_subdirs == 1 ) {
1025 if ( is_maildir("$userdir/$maildir_name") ) {
1026 check_maildir("$userdir/$maildir_name");
1030 elsif ( $maildir_subdirs == 0 ) {
1031 if ( is_maildir($userdir) ) {
1032 check_maildir($userdir);
1041 my ( $dir, $depth ) = @_;
1044 my @depth_check_files;
1045 if ( opendir my $depth_check_dh, $dir ) {
1046 @depth_check_files = readdir($depth_check_dh);
1047 closedir $depth_check_dh;
1049 foreach my $subdir (@depth_check_files) {
1050 next if ($subdir eq '.' || $subdir eq '..');
1051 $subdir = $dir . '/' . $subdir;
1052 next if ( !-d $subdir );
1055 $found = 1 if ( depth_check( $subdir, $depth - 1 ) );
1058 $found = 1 if ( userdir_check($subdir) );
1065 print "Usage: maildir-migrate [options] <maildir>\n\n";
1067 print " --convert Perform conversion\n";
1068 print " --quiet Silence output\n";
1069 print " --overwrite Overwrite existing files\n";
1070 print " --recursive Recursively look through maildir for subaccounts\n";
1071 print " --to-dovecot Conversion is from Courier to Dovecot\n";
1072 print " --to-courier Conversion is from Dovecot to Courier\n";