1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819 |
- #!/usr/bin/perl
- # $Header: /mhub4/sources/imap-tools/imapcopy.pl,v 1.45 2012/03/15 12:35:11 rick Exp $
- #######################################################################
- # Program name imapcopy.pl #
- # Written by Rick Sanders #
- # #
- # Description #
- # #
- # imapcopy is a utility for copying a user's messages from one #
- # IMAP server to another. #
- # #
- # imapcopy is called like this: #
- # ./imapcopy -S host1/user1/password1 -D host2/user2/password2 #
- # #
- # Optional arguments: #
- # -d debug #
- # -I show IMAP protocol exchanges #
- # -L logfile #
- # -m mailbox list (copy only certain mailboxes,see usage notes) #
- # -r reset the \DELETE flag on copied messages #
- # -p <root mailbox> put copied mailboxes under a root mbx #
- # -M <file> mailbox mapping (eg, src:inbox -> dst:inbox_copied) #
- # -i initialize mailbox (remove existing msgs first) #
- # -U run in "update" mode
- # Run imapcopy.pl -h to see complete set of arguments. #
- #######################################################################
- use Socket;
- use FileHandle;
- use Fcntl;
- use Getopt::Std;
- use IO::Socket;
- #################################################################
- # Main program. #
- #################################################################
- init();
- # Get list of all messages on the source host
- #
- connectToHost($sourceHost, \$src) or exit;
- login($sourceUser,$sourcePwd, $sourceHost, $src, $srcMethod) or exit;
- namespace( $src, \$srcPrefix, \$srcDelim, $opt_x );
- connectToHost( $destHost, \$dst ) or exit;
- login( $destUser,$destPwd, $destHost, $dst, $dstMethod ) or exit;
- namespace( $dst, \$dstPrefix, \$dstDelim, $opt_y );
- @mbxs = getMailboxList( $srcPrefix, $src );
- # Exclude certain mbxs if that's what the user wants
- exclude_mbxs( \@mbxs ) if $excludeMbxs;
- map_mbx_names( \%mbx_map, $srcDelim, $dstDelim );
- if ( $archive_mbx ) {
- # Create an archive mbx on the source to receive copies of messsages
- createMbx( $archive_mbx, $src ) unless mbxExists( $archive_mbx, $src);
- }
- $total=$mbxs_processed = 0;
- my $delete_msg_list;
- $num_mbxs = $#mbxs + 1;
- Log("Number of mailboxes to process: $num_mbxs");
- foreach $srcmbx ( @mbxs ) {
- ### encode( \$srcmbx );
- next if $srcmbx eq $archive_mbx;
- $archived=0;
- $mbxs_processed++;
- if ( $verbose ) {
- $line = "Processing $srcmbx " . '(' . $mbxs_processed . '/' . $num_mbxs . ')';
- Log("$line");
- }
- $dstmbx = mailboxName( $srcmbx,$srcPrefix,$srcDelim,$dstPrefix,$dstDelim );
- $LAST = "$dstmbx";
- createMbx( $dstmbx, $dst ) unless mbxExists( $dstmbx, $dst);
- if ( $include_nosel_mbxs ) {
- # If a mailbox was 'Noselect' on the src but the user wants
- # it created as a regular folder on the dst then do so. They
- # don't hold any messages so after creating them we don't need
- # to do anything else.
- next if $nosel_mbxs{"$srcmbx"};
- }
- selectMbx( $dstmbx, $dst );
- if ( $update ) {
- getMsgIdList( $dstmbx, \%DST_MSGS, $dst );
- }
- init_mbx( $dstmbx, $dst ) if $init_mbx;
- $checkpoint = "$srcmbx|$sourceHost|$sourceUser|$sourcePwd|";
- $checkpoint .= "$destHost|$destUser|$destPwd";
-
- if ( $sent_after ) {
- getDatedMsgList( $srcmbx, $sent_after, \@msgs, $src, 'EXAMINE' );
- } else {
- getMsgList( $srcmbx, \@msgs, $src, 'EXAMINE' );
- }
- my $msgcount = $#msgs + 1;
- Log(" Copying $msgcount messages in $srcmbx mailbox") if $verbose;
- if ( $msgcount == 0 ) {
- Log(" $srcmbx mailbox is empty");
- next;
- }
- $copied=0;
- foreach $_ ( @msgs ) {
- alarm $timeout;
- ($msgnum,$date,$flags,$msgid) = split(/\|/, $_);
- Log("msgnum=$msgnum,msgid=$msgid") if $debug;
- if ( $update ) {
- # Don't insert the message if it already exists
- next if $DST_MSGS{"$msgid"};
- }
- # Strip off TZ offset if it exists
- $date =~ s/\((.+)\)$//;
- $date =~ s/\s+$//g;
- $LAST = "$dstmbx|$msgnum";
- next unless fetchMsg( $msgnum, \$message, $srcmbx, $src );
- # $message =~ /Message-Id: (.+)/i;
- # Log("message has msgid = $1");
- alarm 0;
- if ( $conn_timed_out ) {
- Log("$sourceHost timed out");
- reconnect( $checkpoint, $src );
- $conn_timed_out = 0;
- next;
- }
- next unless $message;
- alarm $timeout;
- $copied++ if insertMsg( $dst, $dstmbx, *message, $flags, $date );
- if ( $archive_mbx ) {
- # Put a copy of the message in it too
- if ( insertMsg( $src, $archive_mbx, *message, $flags, $date ) ) {
- $archived++;
- if ( $rem_src_msgs ) {
- $delete_msg_list .= "$msgnum ";
- }
- }
- }
- if ( $copied/100 == int($copied/100)) {
- Log(" Copied $copied messages so far") if $verbose;
- }
- alarm 0;
- if ( $conn_timed_out ) {
- Log("$destHost timed out");
- reconnect( $checkpoint, $dst );
- $conn_timed_out = 0;
- next;
- }
- }
- $total += $copied;
- if ( $use_utf7 ) {
- $dstmbx = Unicode::IMAPUtf7::imap_utf7_decode( $dstmbx );
- }
- if ( $verbose ) {
- $line = " Copied $copied messages to $dstmbx ";
- $line .= '(' . $mbxs_processed . '/' . $num_mbxs . ')';
- Log( "$line ");
- } else {
- Log(" Copied $copied messages to $dstmbx");
- }
- if ( $archive_mbx ) {
- Log(" Copied $archived messages to $archive_mbx mailbox");
- if ( $rem_src_msgs ) {
- # Remove the messages from the source mailbox
- Log("Removing messages from $srcmbx on source");
- delete_msg_list( $delete_msg_list, $srcmbx, $src );
- expungeMbx( $srcmbx, $src );
- }
- }
- }
- Log("Copied $total total messages");
- logout( $src );
- logout( $dst );
- exit;
- sub init {
- $os = $ENV{'OS'};
- processArgs();
- # Open the logFile
- #
- if ( $logfile ) {
- if ( !open(LOG, ">> $logfile")) {
- print STDOUT "Can't open $logfile: $!\n";
- exit;
- }
- select(LOG); $| = 1;
- }
- Log("$0 starting");
- # Determine whether we have SSL support via openSSL and IO::Socket::SSL
- $ssl_installed = 1;
- eval 'use IO::Socket::SSL';
- if ( $@ ) {
- $ssl_installed = 0;
- }
- # Set up signal handling
- $SIG{'ALRM'} = 'signalHandler';
- $SIG{'HUP'} = 'signalHandler';
- $SIG{'INT'} = 'signalHandler';
- $SIG{'TERM'} = 'signalHandler';
- $SIG{'URG'} = 'signalHandler';
- }
- #
- # sendCommand
- #
- # This subroutine formats and sends an IMAP protocol command to an
- # IMAP server on a specified connection.
- #
- sub sendCommand {
- my $fd = shift;
- my $cmd = shift;
- print $fd "$cmd\r\n";
- Log (">> $cmd") if $showIMAP;
- }
- #
- # readResponse
- #
- # This subroutine reads and formats an IMAP protocol response from an
- # IMAP server on a specified connection.
- #
- sub readResponse {
-
- my $fd = shift;
- $response = <$fd>;
- chop $response;
- $response =~ s/\r//g;
- push (@response,$response);
- Log ("<< $response") if $showIMAP;
- }
- #
- # Log
- #
- # This subroutine formats and writes a log message to STDERR.
- #
- sub Log {
-
- my $str = shift;
- # If a logfile has been specified then write the output to it
- # Otherwise write it to STDOUT
- if ( $logfile ) {
- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
- if ($year < 99) { $yr = 2000; }
- else { $yr = 1900; }
- $line = sprintf ("%.2d-%.2d-%d.%.2d:%.2d:%.2d %s\n",
- $mon + 1, $mday, $year + $yr, $hour, $min, $sec,$str);
- print LOG "$line";
- }
- print STDOUT "$str\n" unless $quiet_mode;
- }
- sub createMbx {
- my $mbx = shift;
- my $conn = shift;
- # Create the mailbox if necessary
- sendCommand ($conn, "1 CREATE \"$mbx\"");
- while ( 1 ) {
- readResponse ($conn);
- last if $response =~ /^1 OK/i;
- last if $response =~ /already exists/i;
- if ( $response =~ /^1 NO|^1 BAD|^\* BYE/ ) {
- Log ("Error creating $mbx: $response");
- last;
- }
- if ( $response eq '' or $response =~ /^1 NO/ ) {
- Log ("unexpected CREATE response: >$response<");
- Log("response is NULL");
- resume();
- last;
- }
-
- }
- }
- # insertMsg
- #
- # This routine inserts a message into a user's mailbox
- #
- sub insertMsg {
- local ($conn, $mbx, *message, $flags, $date) = @_;
- local ($lenx);
- $lenx = length($message);
- Log(" Inserting message") if $debug;
- my $mb = $lenx/1000000;
- if ( $max_size and $mb > $max_size ) {
- commafy( \$lenx );
- Log(" Skipping message because its size ($lenx) exceeds the $max_size MB limit");
- return;
- }
- $totalBytes = $totalBytes + $lenx;
- $totalMsgs++;
- $flags = flags( $flags );
- fixup_date( \$date );
- sendCommand ($conn, "1 APPEND \"$mbx\" ($flags) \"$date\" \{$lenx\}");
- readResponse ($conn);
- if ( $response !~ /^\+/ ) {
- Log ("1 unexpected APPEND response: >$response<");
- if ( $response eq '' or $response =~ /^1 NO/ ) {
- Log("response is NULL");
- resume();
- next;
- }
- # next;
- push(@errors,"Error appending message to $mbx for $user");
- return 0;
- }
- print $conn "$message\r\n";
- undef @response;
- while ( 1 ) {
- readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- elsif ( $response !~ /^\*/ ) {
- Log ("unexpected APPEND response: $response");
- # next;
- return 0;
- }
- }
- return 1;
- }
- # Make a connection to a IMAP host
- sub connectToHost {
- my $host = shift;
- my $conn = shift;
- Log("Connecting to $host") if $debug;
-
- ($host,$port) = split(/:/, $host);
- $port = 143 unless $port;
- # We know whether to use SSL for ports 143 and 993. For any
- # other ones we'll have to figure it out.
- $mode = sslmode( $host, $port );
- if ( $mode eq 'SSL' ) {
- unless( $ssl_installed == 1 ) {
- warn("You must have openSSL and IO::Socket::SSL installed to use an SSL connection");
- Log("You must have openSSL and IO::Socket::SSL installed to use an SSL connection");
- exit;
- }
- Log("Attempting an SSL connection") if $debug;
- $$conn = IO::Socket::SSL->new(
- Proto => "tcp",
- SSL_verify_mode => 0x00,
- PeerAddr => $host,
- PeerPort => $port,
- );
- unless ( $$conn ) {
- $error = IO::Socket::SSL::errstr();
- Log("Error connecting to $host: $error");
- exit;
- }
- } else {
- # Non-SSL connection
- Log("Attempting a non-SSL connection") if $debug;
- $$conn = IO::Socket::INET->new(
- Proto => "tcp",
- PeerAddr => $host,
- PeerPort => $port,
- );
- unless ( $$conn ) {
- Log("Error connecting to $host:$port: $@");
- warn "Error connecting to $host:$port: $@";
- exit;
- }
- }
- Log("Connected to $host on port $port");
- }
- sub sslmode {
- my $host = shift;
- my $port = shift;
- my $mode;
- # Determine whether to make an SSL connection
- # to the host. Return 'SSL' if so.
- if ( $port == 143 ) {
- # Standard non-SSL port
- return '';
- } elsif ( $port == 993 ) {
- # Standard SSL port
- return 'SSL';
- }
-
- unless ( $ssl_installed ) {
- # We don't have SSL installed on this machine
- return '';
- }
- # For any other port we need to determine whether it supports SSL
- my $conn = IO::Socket::SSL->new(
- Proto => "tcp",
- SSL_verify_mode => 0x00,
- PeerAddr => $host,
- PeerPort => $port,
- );
- if ( $conn ) {
- close( $conn );
- $mode = 'SSL';
- } else {
- $mode = '';
- }
- return $mode;
- }
- # trim
- #
- # remove leading and trailing spaces from a string
- sub trim {
-
- local (*string) = @_;
- $string =~ s/^\s+//;
- $string =~ s/\s+$//;
- return;
- }
- # login
- #
- # login in at the source host with the user's name and password
- #
- sub login {
- my $user = shift;
- my $pwd = shift;
- my $host = shift;
- my $conn = shift;
- my $method = shift;
- Log("Authenticating to $host as $user");
- if ( uc( $method ) eq 'CRAM-MD5' ) {
- # A CRAM-MD5 login is requested
- Log("login method $method");
- my $rc = login_cram_md5( $user, $pwd, $conn );
- return $rc;
- }
- # Otherwise do a PLAIN login
- sendCommand ($conn, "1 LOGIN $user \"$pwd\"");
- while (1) {
- readResponse ( $conn );
- last if $response =~ /^1 OK/i;
- if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
- Log ("unexpected LOGIN response: $response");
- return 0;
- }
- }
- Log("Logged in as $user") if $debug;
- return 1;
- }
- sub login_cram_md5 {
- my $user = shift;
- my $pwd = shift;
- my $conn = shift;
- sendCommand ($conn, "1 AUTHENTICATE CRAM-MD5");
- while (1) {
- readResponse ( $conn );
- last if $response =~ /^\+/;
- if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
- Log ("unexpected LOGIN response: $response");
- return 0;
- }
- }
- my ($challenge) = $response =~ /^\+ (.+)/;
- Log("challenge $challenge") if $debug;
- $response = cram_md5( $challenge, $user, $pwd );
- Log("response $response") if $debug;
- sendCommand ($conn, $response);
- while (1) {
- readResponse ( $conn );
- last if $response =~ /^1 OK/i;
- if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
- Log ("unexpected LOGIN response: $response");
- return 0;
- }
- }
- Log("Logged in as $user") if $debug;
- return 1;
- }
- # logout
- #
- # log out from the host
- #
- sub logout {
- my $conn = shift;
- undef @response;
- sendCommand ($conn, "1 LOGOUT");
- while ( 1 ) {
- readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- elsif ( $response !~ /^\*/ ) {
- Log ("unexpected LOGOUT response: $response");
- last;
- }
- }
- close $conn;
- return;
- }
- # getMailboxList
- #
- # get a list of the user's mailboxes from the source host
- #
- sub getMailboxList {
- my $prefix = shift;
- my $conn = shift;
- my @mbxs;
- # Get a list of the user's mailboxes
- #
- Log("Get list of user's mailboxes",2) if $debugMode;
- if ( $mbxList ) {
- foreach $mbx ( split(/,/, $mbxList) ) {
- $mbx = $prefix . $mbx if $prefix;
- if ( $opt_R ) {
- # Get all submailboxes under the ones specified
- $mbx .= '*';
- @mailboxes = listMailboxes( $mbx, $conn);
- push( @mbxs, @mailboxes );
- } else {
- push( @mbxs, $mbx );
- }
- }
- } else {
- # Get all mailboxes
- @mbxs = listMailboxes( '*', $conn);
- }
- return @mbxs;
- }
- # exclude_mbxs
- #
- # Exclude certain mailboxes from the list if the user
- # has provided an exclude list with the -e argument
- sub exclude_mbxs {
- my $mbxs = shift;
- my @new_list;
- my %exclude;
- foreach my $exclude ( split(/,/, $excludeMbxs ) ) {
- $exclude{"$exclude"} = 1;
- }
- foreach my $mbx ( @$mbxs ) {
- next if $exclude{"$mbx"};
- push( @new_list, $mbx );
- }
- @$mbxs = @new_list;
- }
- # listMailboxes
- #
- # Get a list of the user's mailboxes
- #
- sub listMailboxes {
- my $mbx = shift;
- my $conn = shift;
- sendCommand ($conn, "1 LIST \"\" \"$mbx\"");
- undef @response;
- while ( 1 ) {
- &readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- elsif ( $response !~ /^\*/ ) {
- &Log ("unexpected response: $response");
- return 0;
- }
- }
- @mbxs = ();
- for $i (0 .. $#response) {
- $response[$i] =~ s/\s+/ /;
- if ( $response[$i] =~ /"$/ ) {
- $response[$i] =~ /\* LIST \((.*)\) "(.+)" "(.+)"/i;
- $mbx = $3;
- } elsif ( $response[$i] =~ /\* LIST \((.*)\) NIL (.+)/i ) {
- $mbx = $2;
- } else {
- $response[$i] =~ /\* LIST \((.*)\) "(.+)" (.+)/i;
- $mbx = $3;
- }
- $mbx =~ s/^\s+//; $mbx =~ s/\s+$//;
- if ($response[$i] =~ /NOSELECT/i) {
- if ( $include_nosel_mbxs ) {
- $nosel_mbxs{"$mbx"} = 1;
- } else {
- Log("$mbx is set NOSELECT, skipping it") if $debug;
- next;
- }
- }
- if ($mbx =~ /^\./) {
- # Skip mailboxes starting with a dot
- next;
- }
- push ( @mbxs, $mbx ) if $mbx ne '';
- }
- return @mbxs;
- }
- # getMsgList
- #
- # Get a list of the user's messages in the indicated mailbox on
- # the source host
- #
- sub getMsgList {
- my $mailbox = shift;
- my $msgs = shift;
- my $conn = shift;
- my $mode = shift;
- my $seen;
- my $empty;
- my $msgnum;
- my $from;
- my $flags;
- my $msgid;
- @$msgs = ();
- $mode = 'EXAMINE' unless $mode;
- sendCommand ($conn, "1 $mode \"$mailbox\"");
- undef @response;
- $empty=0;
- while ( 1 ) {
- readResponse ( $conn );
- if ( $response =~ / 0 EXISTS/i ) { $empty=1; }
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- elsif ( $response !~ /^\*/ ) {
- Log ("unexpected response: $response");
- return 0;
- }
- }
- return 1 if $empty;
- my $start = 1;
- my $end = '*';
- $start = $start_fetch if $start_fetch;
- $end = $end_fetch if $end_fetch;
- sendCommand ( $conn, "1 FETCH $start:$end (uid flags internaldate body[header.fields (From Date Message-Id)])");
-
- @response = ();
- while ( 1 ) {
- readResponse ( $conn );
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- last if $response =~ /^1 NO|^1 BAD|^\* BYE/;
- }
- $flags = '';
- for $i (0 .. $#response) {
- last if $response[$i] =~ /^1 OK FETCH complete/i;
- if ($response[$i] =~ /FLAGS/) {
- # Get the list of flags
- $response[$i] =~ /FLAGS \(([^\)]*)/;
- $flags = $1;
- $flags =~ s/\\Recent//;
- }
- if ( $response[$i] =~ /INTERNALDATE/) {
- $response[$i] =~ /INTERNALDATE (.+) BODY/i;
- # $response[$i] =~ /INTERNALDATE "(.+)" BODY/;
- $date = $1;
-
- $date =~ /"(.+)"/;
- $date = $1;
- $date =~ s/"//g;
- }
- if ( $response[$i] =~ /^Message-Id: (.+)/i ) {
- $msgid = $1;
- }
- # if ( $response[$i] =~ /\* (.+) [^FETCH]/ ) {
- if ( $response[$i] =~ /\* (.+) FETCH/ ) {
- ($msgnum) = split(/\s+/, $1);
- }
- if ( $msgnum and $date and $msgid ) {
- # if ( $msgnum and $date ) {
- push (@$msgs,"$msgnum|$date|$flags|$msgid");
- $msgnum = $date = $msgid = '';
- }
- }
- return 1;
- }
- # getDatedMsgList
- #
- # Get a list of the user's messages in a mailbox on
- # the host which were sent after the specified date
- #
- sub getDatedMsgList {
- my $mailbox = shift;
- my $cutoff_date = shift;
- my $msgs = shift;
- my $conn = shift;
- my $oper = shift;
- my ($seen, $empty, @list,$msgid);
- # Get a list of messages sent after the specified date
- Log("Searching for messages after $cutoff_date");
- @list = ();
- @$msgs = ();
- sendCommand ($conn, "1 $oper \"$mailbox\"");
- while ( 1 ) {
- readResponse ($conn);
- if ( $response =~ / EXISTS/i) {
- $response =~ /\* ([^EXISTS]*)/;
- # Log(" There are $1 messages in $mailbox");
- } elsif ( $response =~ /^1 OK/i ) {
- last;
- } elsif ( $response =~ /^1 NO/i ) {
- Log ("unexpected SELECT response: $response");
- return 0;
- } elsif ( $response !~ /^\*/ ) {
- Log ("unexpected SELECT response: $response");
- return 0;
- }
- }
- my ($date,$ts) = split(/\s+/, $cutoff_date);
- #
- # Get list of messages sent before the reference date
- #
- Log("Get messages sent after $date") if $debug;
- $nums = "";
- sendCommand ($conn, "1 SEARCH SINCE \"$date\"");
- while ( 1 ) {
- readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- elsif ( $response =~ /^\*\s+SEARCH/i ) {
- ($nums) = ($response =~ /^\*\s+SEARCH\s+(.*)/i);
- }
- elsif ( $response !~ /^\*/ ) {
- Log ("unexpected SEARCH response: $response");
- return;
- }
- }
- Log("$nums") if $debug;
- if ( $nums eq "" ) {
- Log (" $mailbox has no messages sent before $date") if $debug;
- return;
- }
- my @number = split(/\s+/, $nums);
- $n = $#number + 1;
- $nums =~ s/\s+/ /g;
- @msgList = ();
- @msgList = split(/ /, $nums);
- if ($#msgList == -1) {
- # No msgs in this mailbox
- return 1;
- }
- @$msgs = ();
- for $num (@msgList) {
- sendCommand ( $conn, "1 FETCH $num (uid flags internaldate body[header.fields (Message-Id Date)])");
-
- undef @response;
- while ( 1 ) {
- readResponse ( $conn );
- Log("RESP1 $response");
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- last if $response =~ /^1 NO|^1 BAD|^\* BYE/;
- }
- $flags = '';
- my $msgid;
- foreach $_ ( @response ) {
- last if /^1 OK FETCH complete/i;
- if ( /FLAGS/ ) {
- # Get the list of flags
- /FLAGS \(([^\)]*)/;
- $flags = $1;
- $flags =~ s/\\Recent//;
- }
-
- if ( /Message-Id:\s*(.+)/i ) {
- $msgid = $1;
- }
- if ( /INTERNALDATE/) {
- /INTERNALDATE (.+) BODY/i;
- $date = $1;
- $date =~ /"(.+)"/;
- $date = $1;
- $date =~ s/"//g;
- #### next if check_cutoff_date( $date, $cutoff_date );
- }
- if ( /\* (.+) FETCH/ ) {
- ($msgnum) = split(/\s+/, $1);
- }
- if ( $msgnum and $date ) {
- push (@$msgs,"$msgnum|$date|$flags|$msgid");
- $msgnum=$msgid=$date=$flags='';
- }
- }
- }
- foreach $_ ( @$msgs ) {
- Log("getDated found $_") if $debug;
- }
- return 1;
- }
- sub mbxExists {
- my $mbx = shift;
- my $conn = shift;
- my $status = 1;
- # Determine whether a mailbox exists
- sendCommand ($conn, "1 EXAMINE \"$mbx\"");
- while (1) {
- readResponse ($conn);
- last if $response =~ /^1 OK/i;
- if ( $response =~ /^1 NO|^1 BAD|^\* BYE/ ) {
- $status = 0;
- last;
- }
- }
- return $status;
- }
- sub fetchMsg {
- my $msgnum = shift;
- my $message = shift;
- my $mbx = shift;
- my $conn = shift;
- Log(" Fetching msg $msgnum...") if $debug;
- $$message = '';
- sendCommand( $conn, "1 FETCH $msgnum (rfc822)");
- while (1) {
- readResponse ($conn);
- last if $response =~ /^1 NO|^1 BAD|^\* BYE/;
- if ( $response eq '' ) {
- Log("RESP2 >$response<");
- resume();
- return 0;
- }
- if ( $response =~ /^1 OK/i ) {
- $size = length($$message);
- last;
- }
- elsif ($response =~ /message number out of range/i) {
- Log ("Error fetching uid $uid: out of range",2);
- $stat=0;
- last;
- }
- elsif ($response =~ /Bogus sequence in FETCH/i) {
- Log ("Error fetching uid $uid: Bogus sequence in FETCH",2);
- $stat=0;
- last;
- }
- elsif ( $response =~ /message could not be processed/i ) {
- Log("Message could not be processed, skipping it ($user,msgnum $msgnum,$destMbx)");
- push(@errors,"Message could not be processed, skipping it ($user,msgnum $msgnum,$destMbx)");
- $stat=0;
- last;
- }
- elsif
- ($response =~ /^\*\s+$msgnum\s+FETCH\s+\(.*RFC822\s+\{[0-9]+\}/i) {
- ($len) = ($response =~ /^\*\s+$msgnum\s+FETCH\s+\(.*RFC822\s+\{([0-9]+)\}/i);
- $cc = 0;
- $$message = "";
- while ( $cc < $len ) {
- $n = 0;
- $n = read ($conn, $segment, $len - $cc);
- if ( $n == 0 ) {
- Log ("unable to read $len bytes");
- resume();
- return 0;
- }
- $$message .= $segment;
- $cc += $n;
- }
- }
- }
- return 1;
- }
- sub usage {
- print STDOUT "usage:\n";
- print STDOUT " imapcopy -S sourceHost/sourceUser/sourcePassword [/CRAM-MD5]\n";
- print STDOUT " -D destHost/destUser/destPassword [/CRAM-MD5]\n";
- print STDOUT " -d debug\n";
- print STDOUT " -I show IMAP protocol exchanges\n";
- print STDOUT " -L logfile\n";
- print STDOUT " -m mailbox list (eg \"Inbox, Drafts, Notes\". Default is all mailboxes)\n";
- print STDOUT " -R include submailboxes when used with -m\n\n";
- print STDOUT " -e exclude mailbox list\n";
- print STDOUT " -r remove msgs from source mbx after copying\n";
- print STDOUT " -p <mailbox> put copied mailboxes under a root mailbox\n";
- print STDOUT " -A <mailbox> copy to local mailbox from scrmbx\n";
- print STDOUT " -x <mbx delimiter [mbx prefix]> source (eg, -x '. INBOX.'\n";
- print STDOUT " -y <mbx delimiter [mbx prefix]> destination\n";
- print STDOUT " -i initialize mailbox (remove existing messages first\n";
- print STDOUT " -M <file> mailbox map file. Maps src mbxs to dst mbxs. ";
- print STDOUT "Each line in the file should be 'src mbx:dst mbx'\n";
- print STDOUT " -q quiet mode (still writes to the logfile)\n";
- print STDOUT " -t <timeout in seconds>\n";
- print STDOUT " -T copy custom flags (eg, \$Label1,\$MDNSent,etc)\n";
- print STDOUT " -a <DD-MMM-YYYY> copy only messages after this date\n";
- print STDOUT " -X <megabytes> Skip any message exceeding this size\n";
- print STDOUT " -U update mode, don't copy messages that already exist\n";
- print STDOUT " -B <msgnum> Starting point for message fetch\n";
- print STDOUT " -E <msgnum> Ending point for message fetch\n";
- exit;
- }
- sub processArgs {
- if ( !getopts( "dS:D:L:m:hIp:M:rqx:y:e:Rt:Tia:X:vP:A:UB:E:" ) ) {
- usage();
- }
- if ( $opt_S =~ /\\/ ) {
- ($sourceHost, $sourceUser, $sourcePwd,$srcMethod) = split(/\\/, $opt_S);
- } else {
- ($sourceHost, $sourceUser, $sourcePwd,$srcMethod) = split(/\//, $opt_S);
- }
- if ( $opt_D =~ /\\/ ) {
- ($destHost, $destUser, $destPwd,$dstMethod) = split(/\\/, $opt_D);
- } else {
- ($destHost, $destUser, $destPwd,$dstMethod) = split(/\//, $opt_D);
- }
- $mbxList = $opt_m;
- $logfile = $opt_L;
- $root_mbx = $opt_p;
- $timeout = $opt_t;
- $tags = $opt_T;
- $debug = 1 if $opt_d;
- $verbose = 1 if $opt_v;
- $showIMAP = 1 if $opt_I;
- $submbxs = 1 if $opt_R;
- $init_mbx = 1 if $opt_i;
- $quiet_mode = 1 if $opt_q;
- $update = 1 if $opt_U;
- $include_nosel_mbxs = 1 if $opt_s;
- $rem_src_msgs = 1 if $opt_r;
- $mbx_map_fn = $opt_M;
- $excludeMbxs = $opt_e;
- $sent_after = $opt_a;
- $max_size = $opt_X;
- $public_mbxs = $opt_P;
- $archive_mbx = $opt_A;
- $start_fetch = $opt_B;
- $end_fetch = $opt_E;
- $timeout = 300 unless $timeout;
- Log("Running in update mode") if $update;
- validate_date( $sent_after ) if $sent_after;
- usage() if $opt_h;
- }
- sub selectMbx {
- my $mbx = shift;
- my $conn = shift;
- # Some IMAP clients such as Outlook and Netscape) do not automatically list
- # all mailboxes. The user must manually subscribe to them. This routine
- # does that for the user by marking the mailbox as 'subscribed'.
- sendCommand( $conn, "1 SUBSCRIBE \"$mbx\"");
- while ( 1 ) {
- readResponse( $conn );
- if ( $response =~ /^1 OK/i ) {
- Log("Mailbox $mbx has been subscribed") if $debug;
- last;
- } elsif ( $response =~ /^1 NO|^1 BAD|\^* BYE/i ) {
- Log("Unexpected response to subscribe $mbx command: $response");
- last;
- }
- }
- # Now select the mailbox
- sendCommand( $conn, "1 SELECT \"$mbx\"");
- while ( 1 ) {
- readResponse( $conn );
- if ( $response =~ /^1 OK/i ) {
- last;
- } elsif ( $response =~ /^1 NO|^1 BAD|^\* BYE/i ) {
- Log("Unexpected response to SELECT $mbx command: $response");
- last;
- }
- }
- }
- sub namespace {
- my $conn = shift;
- my $prefix = shift;
- my $delimiter = shift;
- my $mbx_delim = shift;
- # Query the server with NAMESPACE so we can determine its
- # mailbox prefix (if any) and hierachy delimiter.
- if ( $mbx_delim ) {
- # The user has supplied a mbx delimiter and optionally a prefix.
- Log("Using user-supplied mailbox hierarchy delimiter $mbx_delim");
- ($$delimiter,$$prefix) = split(/\s+/, $mbx_delim);
- return;
- }
- @response = ();
- sendCommand( $conn, "1 NAMESPACE");
- while ( 1 ) {
- readResponse( $conn );
- if ( $response =~ /^1 OK/i ) {
- last;
- } elsif ( $response =~ /^1 NO|^1 BAD|^\* BYE/i ) {
- Log("Unexpected response to NAMESPACE command: $response");
- last;
- }
- }
- foreach $_ ( @response ) {
- if ( /NAMESPACE/i ) {
- my $i = index( $_, '((' );
- my $j = index( $_, '))' );
- my $val = substr($_,$i+2,$j-$i-3);
- ($val) = split(/\)/, $val);
- ($$prefix,$$delimiter) = split( / /, $val );
- $$prefix =~ s/"//g;
- $$delimiter =~ s/"//g;
-
- # Experimental
- if ( $public_mbxs ) {
- # Figure out the public mailbox settings
- /\(\((.+)\)\)\s+\(\((.+)\s+\(\((.+)\)\)/;
- $public = $3;
- $public =~ /"(.+)"\s+"(.+)"/;
- $src_public_prefix = $1 if $conn eq $src;
- $src_public_delim = $2 if $conn eq $src;
- $dst_public_prefix = $1 if $conn eq $dst;
- $dst_public_delim = $2 if $conn eq $dst;
- }
- last;
- }
- last if /^1 NO|^1 BAD|^\* BYE/;
- }
- unless ( $$delimiter ) {
- # NAMESPACE command is not supported by the server
- # so we will have to figure it out another way.
- $delim = getDelimiter( $conn );
- $$delimiter = $delim;
- $$prefix = '';
- }
- if ( $debug ) {
- Log("prefix >$$prefix<");
- Log("delim >$$delimiter<");
- }
- }
- sub mailboxName {
- my $srcmbx = shift;
- my $srcPrefix = shift;
- my $srcDelim = shift;
- my $dstPrefix = shift;
- my $dstDelim = shift;
- my $dstmbx;
- my $substChar = '_';
- if ( $public_mbxs ) {
- my ($public_src,$public_dst) = split(/:/, $public_mbxs );
- # If the mailbox starts with the public mailbox prefix then
- # map it to the public mailbox destination prefix
- if ( $srcmbx =~ /^$public_src/ ) {
- Log("src: $srcmbx is a public mailbox") if $debug;
- $dstmbx = $srcmbx;
- $dstmbx =~ s/$public_src/$public_dst/;
- Log("dst: $dstmbx") if $debug;
- return $dstmbx;
- }
- }
- # Change the mailbox name if the user has supplied mapping rules.
- if ( $mbx_map{"$srcmbx"} ) {
- $srcmbx = $mbx_map{"$srcmbx"}
- }
- # Adjust the mailbox name if the source and destination server
- # have different mailbox prefixes or hierarchy delimiters.
- if ( ($srcmbx =~ /[$dstDelim]/) and ($dstDelim ne $srcDelim) ) {
- # The mailbox name has a character that is used on the destination
- # as a mailbox hierarchy delimiter. We have to replace it.
- $srcmbx =~ s^[$dstDelim]^$substChar^g;
- }
- if ( $debug ) {
- Log("src mbx $srcmbx");
- Log("src prefix $srcPrefix");
- Log("src delim $srcDelim");
- Log("dst prefix $dstPrefix");
- Log("dst delim $dstDelim");
- }
- $srcmbx =~ s/^$srcPrefix//;
- $srcmbx =~ s/\\$srcDelim/\//g;
-
- if ( ($srcPrefix eq $dstPrefix) and ($srcDelim eq $dstDelim) ) {
- # No adjustments necessary
- # $dstmbx = $srcmbx;
- if ( lc( $srcmbx ) eq 'inbox' ) {
- $dstmbx = $srcmbx;
- } else {
- $dstmbx = $srcPrefix . $srcmbx;
- }
- if ( $root_mbx ) {
- # Put folders under a 'root' folder on the dst
- $dstmbx =~ s/^$dstPrefix//;
- $dstDelim =~ s/\./\\./g;
- $dstmbx =~ s/^$dstDelim//;
- $dstmbx = $dstPrefix . $root_mbx . $dstDelim . $dstmbx;
- if ( uc($srcmbx) eq 'INBOX' ) {
- # Special case for the INBOX
- $dstmbx =~ s/INBOX$//i;
- $dstmbx =~ s/$dstDelim$//;
- }
- $dstmbx =~ s/\\//g;
- }
- return $dstmbx;
- }
- $srcmbx =~ s#^$srcPrefix##;
- $dstmbx = $srcmbx;
- if ( $srcDelim ne $dstDelim ) {
- # Need to substitute the dst's hierarchy delimiter for the src's one
- $srcDelim = '\\' . $srcDelim if $srcDelim eq '.';
- $dstDelim = "\\" . $dstDelim if $dstDelim eq '.';
- $dstmbx =~ s#$srcDelim#$dstDelim#g;
- $dstmbx =~ s/\\//g;
- }
- if ( $srcPrefix ne $dstPrefix ) {
- # Replace the source prefix with the dest prefix
- $dstmbx =~ s#^$srcPrefix## if $srcPrefix;
- if ( $dstPrefix ) {
- $dstmbx = "$dstPrefix$dstmbx" unless uc($srcmbx) eq 'INBOX';
- }
- $dstDelim = "\\$dstDelim" if $dstDelim eq '.';
- $dstmbx =~ s#^$dstDelim##;
- }
-
- if ( $root_mbx ) {
- # Put folders under a 'root' folder on the dst
- $dstDelim =~ s/\./\\./g;
- $dstmbx =~ s/^$dstPrefix//;
- $dstmbx =~ s/^$dstDelim//;
- $dstmbx = $dstPrefix . $root_mbx . $dstDelim . $dstmbx;
- if ( uc($srcmbx) eq 'INBOX' ) {
- # Special case for the INBOX
- $dstmbx =~ s/INBOX$//i;
- $dstmbx =~ s/$dstDelim$//;
- }
- $dstmbx =~ s/\\//g;
- }
- return $dstmbx;
- }
- sub flags {
- my $flags = shift;
- my @newflags;
- my $newflags;
- # Make sure the flags list contains standard
- # IMAP flags and optionally custom tags
- return unless $flags;
- $flags =~ s/\\Recent//i;
- foreach $_ ( split(/\s+/, $flags) ) {
- push( @newflags, $_ ) if substr($_,0,1) eq '\\';
- if ( $opt_T ) {
- # Include user-defined flags
- push( @newflags, $_ ) if substr($_,0,1) eq '$';
- }
- }
- $newflags = join( ' ', @newflags );
- $newflags =~ s/\\Deleted//ig if $opt_r;
- $newflags =~ s/^\s+|\s+$//g;
- return $newflags;
- }
- sub map_mbx_names {
- my $mbx_map = shift;
- my $srcDelim = shift;
- my $dstDelim = shift;
- # The -M <file> argument causes imapcopy to read the
- # contents of a file with mappings between source and
- # destination mailbox names. This permits the user to
- # to change the name of a mailbox when copying messages.
- #
- # The lines in the file should be formatted as:
- # <source mailbox name>: <destination mailbox name>
- # For example:
- # Drafts/2008/Save: Draft_Messages/2008/Save
- # Action Items: Inbox
- #
- # Note that if the names contain non-ASCII characters such
- # as accents or diacritical marks then the Perl module
- # Unicode::IMAPUtf7 module must be installed.
- return unless $mbx_map_fn;
- unless ( open(MAP, "<$mbx_map_fn") ) {
- Log("Error opening mbx map file $mbx_map_fn: $!");
- exit;
- }
- $use_utf7 = 0;
- while( <MAP> ) {
- chomp;
- s/[\r\n]$//; # In case we're on Windows
- s/^\s+//;
- next if /^#/;
- next unless $_;
- ($srcmbx,$dstmbx) = split(/\s*:\s*/, $_);
- # Unless the mailbox name is entirely ASCII we'll have to use
- # the Modified UTF-7 character set.
- $use_utf7 = 1 unless isAscii( $srcmbx );
- $use_utf7 = 1 unless isAscii( $dstmbx );
- $srcmbx =~ s/\//$srcDelim/g;
- $dstmbx =~ s/\//$dstDelim/g;
- $$mbx_map{"$srcmbx"} = $dstmbx;
- }
- close MAP;
- if ( $use_utf7 ) {
- eval 'use Unicode::IMAPUtf7';
- if ( $@ ) {
- Log("At least one mailbox map contains non-ASCII characters. This means you");
- Log("have to install the Perl Unicode::IMAPUtf7 module in order to map mailbox ");
- Log("names between the source and destination servers.");
- print "At least one mailbox map contains non-ASCII characters. This means you\n";
- print "have to install the Perl Unicode::IMAPUtf7 module in order to map mailbox\n";
- print "names between the source and destination servers.\n";
- exit;
- }
- }
- my %temp;
- foreach $srcmbx ( keys %$mbx_map ) {
- $dstmbx = $$mbx_map{"$srcmbx"};
- Log("Mapping src:$srcmbx to dst:$dstmbx");
- if ( $use_utf7 ){
- # Encode the name in Modified UTF-7 charset
- $srcmbx = Unicode::IMAPUtf7::imap_utf7_encode( $srcmbx );
- $dstmbx = Unicode::IMAPUtf7::imap_utf7_encode( $dstmbx );
- }
- $temp{"$srcmbx"} = $dstmbx;
- }
- %$mbx_map = %temp;
- %temp = ();
- }
- sub isAscii {
- my $str = shift;
- my $ascii = 1;
- # Determine whether a string contains non-ASCII characters
- my $test = $str;
- $test=~s/\P{IsASCII}/?/g;
- $ascii = 0 unless $test eq $str;
- return $ascii;
- }
- sub getDelimiter {
- my $conn = shift;
- my $delimiter;
- # Issue a 'LIST "" ""' command to find out what the
- # mailbox hierarchy delimiter is.
- sendCommand ($conn, '1 LIST "" ""');
- @response = '';
- while ( 1 ) {
- readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- last;
- }
- elsif ( $response !~ /^\*/ ) {
- Log ("unexpected response: $response");
- return 0;
- }
- }
- for $i (0 .. $#response) {
- $response[$i] =~ s/\s+/ /;
- if ( $response[$i] =~ /\* LIST \((.*)\) "(.*)" "(.*)"/i ) {
- $delimiter = $2;
- }
- }
- return $delimiter;
- }
- # Reconnect to the servers after a timeout error.
- #
- sub reconnect {
- my $checkpoint = shift;
- my $conn = shift;
- Log("Attempting to reconnect");
- my ($mbx,$shost,$suser,$spwd,$dhost,$duser,$dpwd) = split(/\|/, $checkpoint);
- close $src;
- close $dst;
- connectToHost($shost,\$src);
- login($suser,$spwd,$shost,$src);
- connectToHost($dhost,\$dst);
- login($duser,$dpwd,$dhost,$dst);
- selectMbx( $mbx, $src );
- createMbx( $mbx, $dst ); # Just in case
- }
- # Handle signals
- sub signalHandler {
- my $sig = shift;
- if ( $sig eq 'ALRM' ) {
- Log("Caught a SIG$sig signal, timeout error");
- $conn_timed_out = 1;
- } else {
- Log("Caught a SIG$sig signal, shutting down");
- exit;
- }
- Log("Resuming");
- }
- sub fixup_date {
- my $date = shift;
- # Make sure the hrs part of the date is 2 digits. At least
- # one IMAP server expects this.
- $$date =~ s/^\s+//;
- $$date =~ /(.+) (.+):(.+):(.+) (.+)/;
- my $hrs = $2;
-
- return if length( $hrs ) == 2;
- my $newhrs = '0' . $hrs if length( $hrs ) == 1;
- $$date =~ s/ $hrs/ $newhrs/;
- }
- sub init_mbx {
- my $mbx = shift;
- my $conn = shift;
- my @msgs;
- # Remove all messages from a mailbox
- Log("Initializing mailbox $mbx");
- getMsgList( $mbx, \@msgs, $conn, 'SELECT' );
- my $msgcount = $#msgs + 1;
- Log("$mbx has $msgcount messages");
- return if $msgcount == 0; # No messages to delete
- foreach my $msgnum ( @msgs ) {
- ($msgnum) = split(/\|/, $msgnum);
- delete_msg( $msgnum, $conn );
- }
- expungeMbx( $mbx, $conn );
- }
- sub delete_msg_list {
- my $msgnums = shift;
- my $mbx = shift;
- my $conn = shift;
- my $rc;
- # Mark a set of messages for deletion
- selectMbx( $mbx, $conn );
- foreach my $msgnum ( split(/\s+/, $msgnums ) ) {
- sendCommand ( $conn, "1 STORE $msgnum +FLAGS (\\Deleted)");
- while (1) {
- readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- $rc = 1;
- Log(" Marked msg number $msgnum for delete") if $debug;
- last;
- }
- if ( $response =~ /^1 BAD|^1 NO/i ) {
- Log("Error setting \Deleted flag for msg $msgnum: $response");
- $rc = 0;
- last;
- }
- }
- }
- return $rc;
- }
- sub expungeMbx {
- my $mbx = shift;
- my $conn = shift;
- Log("Expunging mailbox $mbx");
- sendCommand ($conn, "1 SELECT \"$mbx\"");
- while (1) {
- readResponse ($conn);
- last if ( $response =~ /1 OK/i );
- }
- sendCommand ( $conn, "1 EXPUNGE");
- $expunged=0;
- while (1) {
- readResponse ($conn);
- $expunged++ if $response =~ /\* (.+) Expunge/i;
- last if $response =~ /^1 OK/;
- if ( $response =~ /^1 BAD|^1 NO/i ) {
- Log("Error purging messages: $response");
- last;
- }
- }
- $totalExpunged += $expunged;
- Log("$expunged messages expunged");
- }
- sub cram_md5 {
- my $challenge = shift;
- my $user = shift;
- my $password = shift;
- eval 'use Digest::HMAC_MD5 qw(hmac_md5_hex)';
- use MIME::Base64 qw(decode_base64 encode_base64);
- # Adapated from script by Paul Makepeace <http://paulm.com>, 2002-10-12
- # Takes user, key, and base-64 encoded challenge and returns base-64
- # encoded CRAM. See,
- # IMAP/POP AUTHorize Extension for Simple Challenge/Response:
- # RFC 2195 http://www.faqs.org/rfcs/rfc2195.html
- # SMTP Service Extension for Authentication:
- # RFC 2554 http://www.faqs.org/rfcs/rfc2554.html
- # Args: tim tanstaaftanstaaf PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
- # should yield: dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
- my $challenge_data = decode_base64($challenge);
- my $hmac_digest = hmac_md5_hex($challenge_data, $password);
- my $response = encode_base64("$user $hmac_digest");
- chomp $response;
- if ( $debug ) {
- Log("Challenge: $challenge_data");
- Log("HMAC digest: $hmac_digest");
- Log("CRAM Base64: $response");
- }
- return $response;
- }
- sub validate_date {
- my $date = shift;
- my $invalid;
- # Make sure the "after" date is in DD-MMM-YYYY format
- my ($day,$month,$year) = split(/-/, $date);
- $invalid = 1 unless ( $day > 0 and $day < 32 );
- $invalid = 1 unless $month =~ /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/i;
- $invalid = 1 unless $year > 1900 and $year < 2999;
- if ( $invalid ) {
- Log("The 'Sent after' date $date must be in DD-MMM-YYYY format");
- exit;
- }
- }
- sub commafy {
- my $number = shift;
- $_ = $$number;
- 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
- $$number = $_;
- }
- sub delete_msg {
- my $msgnum = shift;
- my $conn = shift;
- my $rc;
- sendCommand ( $conn, "1 STORE $msgnum +FLAGS (\\Deleted)");
- while (1) {
- readResponse ($conn);
- if ( $response =~ /^1 OK/i ) {
- $rc = 1;
- Log(" Marked msg number $msgnum for delete") if $debug;
- last;
- }
- if ( $response =~ /^1 BAD|^1 NO/i ) {
- Log("Error setting \Deleted flag for msg $msgnum: $response");
- $rc = 0;
- last;
- }
- }
- return $rc;
- }
- sub resume {
- # Disconnect, re-connect, and log back in.
- Log("Fatal error, lost connection to either the source or destination");
- # Log("checkpoint $checkpoint");
- Log("LAST $LAST");
- my ($mbx,$msgnum) = split(/\|/, $LAST);
- Log("mbx $mbx");
- Log("Disconnect from the source and destination servers");
- close $src;
- close $dst;
-
- Log("Sleeping 15 seconds before reconnecting");
- sleep 15;
- Log("Reconnect to source server and log back in");
- connectToHost($sourceHost, \$src) or exit;
- login($sourceUser,$sourcePwd, $sourceHost, $src, $srcMethod) or exit;
- selectMbx( $mbx, $src );
- Log("Reconnect to destination server and log back in");
- connectToHost( $destHost, \$dst ) or exit;
- login( $destUser,$destPwd, $destHost, $dst, $dstMethod ) or exit;
- Log("Resuming");
- # Just in case we were creating a mailbox when the connection
- # was lost check and recreate it if necessary
- Log("does $mbx exist?");
- createMbx( $mbx, $dst ) unless mbxExists( $mbx, $dst );
- return;
- }
- # getMsgIdList
- #
- # Get a list of the user's messages in a mailbox
- #
- sub getMsgIdList {
- my $mailbox = shift;
- my $msgids = shift;
- my $conn = shift;
- my $empty;
- my $msgnum;
- my $from;
- my $msgid;
- %$msgids = ();
- sendCommand ($conn, "1 EXAMINE \"$mailbox\"");
- undef @response;
- $empty=0;
- while ( 1 ) {
- readResponse ( $conn );
- if ( $response =~ / 0 EXISTS/i ) { $empty=1; }
- if ( $response =~ /^1 OK/i ) {
- # print STDERR "response $response\n";
- last;
- }
- elsif ( $response !~ /^\*/ ) {
- Log ("unexpected response: $response");
- # print STDERR "Error: $response\n";
- return 0;
- }
- }
- if ( $empty ) {
- return;
- }
- Log("Fetch the header info") if $debug;
- sendCommand ( $conn, "1 FETCH 1:* (body[header.fields (Message-Id)])");
- undef @response;
- while ( 1 ) {
- readResponse ( $conn );
- return if $conn_timed_out;
- if ( $response =~ /^1 OK/i ) {
- last;
- } elsif ( $response =~ /could not be processed/i ) {
- Log("Error: response from server: $response");
- return;
- } elsif ( $response =~ /^1 NO|^1 BAD/i ) {
- return;
- }
- }
- $flags = '';
- for $i (0 .. $#response) {
- $_ = $response[$i];
- last if /OK FETCH complete/;
- if ($response[$i] =~ /Message-ID: (.+)/i) {
- $$msgids{"$1"} = 1;
- }
- }
- }
- sub encode_ampersand {
- my $mbx = shift;
- # The IMAP RFC requires mailbox names with '&' be
- # encoded as '&-'
- # The problem with this routine is a mailbox name may be
- # encoded in Mod UTF7 which uses the '&' character for its
- # own purposes, eg r&AOk-pertoire_XXX. We have to leave it
- # alone. Anyway, this code was inserted because of an IMAP
- # server which did not do its job so the usefulness of this
- # conversion is limited.
- if ( $$mbx =~ /\&/ ) {
- if ( $$mbx !~ /\&-/ ) {
- # Need to encode the '&' as '&-'
- $$mbx =~ s/\&/\&-/g;
- Log("Encoded $$mbx");
- }
- }
- }
|