#!perl use warnings; use strict; use Mail::Box::Manager; my $mgr = Mail::Box::Manager->new( 'default_folder_type' => 'mbox' ) ; foreach my $mb ( @ARGV ) { my $mbox = $mgr->open( 'folder' => $mb ) or do { warn "Can't open $mb"; next; }; my $threads = $mgr->threads ( 'folders' => [ $mbox ] , 'timespan' => 'EVER' , 'window' => 'ALL' ); save_thread( $_ ) for $threads->all ; $mgr->close( $mbox ); } { my ( $count , @stat ); sub save_thread { my ( $thread ) = @_; # Generate file name. my $file = 'thread-' . sprintf '%05d' , ++$count; my $save = $mgr->open( 'folder' => $file , 'access' => 'rw' , 'create' => 1) or die "Cannot open $file to save the thread.\n" ; push @stat , [ $count , $thread->numberOfMessages ]; $_->copyTo( $save ) for $thread->threadMessages ; $mgr->close( $save ); } sub END { print_stat(); } sub print_stat { my $out = ''; my ( $total_thr , $total_msg ) = ( 0 ) x2; foreach my $s ( @stat ) { $out .= sprintf "%4d : %2d\n" , @{ $s }; $total_thr++; $total_msg += $s->[1]; } my $old = select STDERR; printf "Threads: %4d, Messages: %4d\n%s\n" , $total_thr , $total_msg , join '' , qw( =- ) x20 ; print $out; select $old; } }