#!/usr/bin/perl # $Header: /mhub4/sources/imap-tools/imapsync.pl,v 1.2 2008/10/23 04:58:48 rick Exp $ ####################################################################### # Program name imapsync.pl # # Written by Rick Sanders # # # # Description # # # # imapsync is a utility for synchronizing a user's account on two # # IMAP servers. When supplied with host/user/password information # # for two IMAP hosts imapsync does the following: # # 1. Adds any messages on the 1st host which aren't on the 2nd # # 2. Deletes any messages from the 2nd which aren't on the 1st # # 3. Sets the message flags on the 2nd to match the 1st's flags# # # # imapsync is called like this: # # ./imapsync -S host1/user1/password1 -D host2/user2/password2 # # # # Optional arguments: # # -d debug # # -L logfile # # -m mailbox list (sync only certain mailboxes,see usage notes) # ####################################################################### use Socket; use FileHandle; use Fcntl; use Getopt::Std; ################################################################# # Main program. # ################################################################# init(); # Get list of all messages on the source host by Message-Id # connectToHost($sourceHost, \$src) or exit; login($sourceUser,$sourcePwd, $src) or exit; namespace( $src, \$srcPrefix, \$srcDelim ); @mbxs = getMailboxList($sourceUser, $src); foreach $mbx ( @mbxs ) { getMsgList( $mbx, \@sourceMsgs, $src ); } # Get list of all messages on the destination host by Message-Id connectToHost( $destHost, \$dst ) or exit; login( $destUser,$destPwd, $dst ) or exit; namespace( $dst, \$dstPrefix, \$dstDelim ); @mbxs=(); @mbxs = getMailboxList($destUser, $dst ); foreach $mbx ( @mbxs ) { $dstmbx = mailboxName( $mbx,$srcPrefix,$srcDelim,$dstPrefix,$dstDelim ); getMsgList( $dstmbx, \@destMsgs, $dst ); } # Build two arrays of messages, one for the source and one for the dest foreach $entry ( @sourceMsgs ) { ($msgid,$mbx,$rest) = split(/\|\|\|\|\|\|/, $entry,3); print STDOUT " SRC $mbx $msgid\n" if $debug; $sourceList{"$msgid\|\|\|\|\|\|$mbx"} = $rest; } foreach $entry ( @destMsgs ) { ($msgid,$mbx,$rest) = split(/\|\|\|\|\|\|/, $entry,3); print STDOUT " DEST $mbx $msgid\n" if $debug; $destList{"$msgid\|\|\|\|\|\|$mbx"} = $rest; } @destkeys = keys( %destList ); @sourcekeys = keys( %sourceList ); # Add any messages in the source which are not in the dest $added = checkForAdds(); # Update the message flags if they have changed on the source $updated = checkForUpdates(); # Delete any messages in the dest which are not in the source $deleted = checkForDeletes(); logout( $src ); logout( $dst ); Log("\nSummary of results"); Log(" Added $added"); Log(" Updated $updated"); Log(" Deleted $deleted"); exit; sub init { $os = $ENV{'OS'}; processArgs(); $timeout = 60 unless $timeout; # Open the logFile # if ( $logfile ) { if ( !open(LOG, ">> $logfile")) { print STDOUT "Can't open $logfile: $!\n"; } select(LOG); $| = 1; } Log("$0 starting\n"); # Determine whether we have SSL support via openSSL and IO::Socket::SSL $ssl_installed = 1; eval 'use IO::Socket::SSL'; if ( $@ ) { $ssl_installed = 0; } } # # sendCommand # # This subroutine formats and sends an IMAP protocol command to an # IMAP server on a specified connection. # sub sendCommand { local($fd) = shift @_; local($cmd) = shift @_; print $fd "$cmd\r\n"; if ($showIMAP) { Log (">> $cmd",2); } } # # readResponse # # This subroutine reads and formats an IMAP protocol response from an # IMAP server on a specified connection. # sub readResponse { local($fd) = shift @_; $response = <$fd>; chop $response; $response =~ s/\r//g; push (@response,$response); if ($showIMAP) { Log ("<< $response",2); } } # # Log # # This subroutine formats and writes a log message to STDERR. # sub Log { my $str = shift; # If a logile 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 %s\n", $mon + 1, $mday, $year + $yr, $hour, $min, $sec,$$,$str); print LOG "$line"; } else { print STDOUT "$str\n"; } print STDOUT "$str\n"; } # insertMsg # # This routine inserts an RFC822 messages into a user's folder # sub insertMsg { local ($conn, $mbx, *message, $flags, $date) = @_; local ($lenx); Log(" Inserting message") if $debug; $lenx = length($message); $totalBytes = $totalBytes + $lenx; $totalMsgs++; $flags = flags( $flags ); sendCommand ($conn, "1 APPEND \"$mbx\" ($flags) \"$date\" \{$lenx\}"); readResponse ($conn); if ( $response !~ /^\+/ ) { Log ("unexpected APPEND response: $response"); # 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 an 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"); warn("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 $conn = shift; $rsn = 1; sendCommand ($conn, "$rsn LOGIN $user $pwd"); while (1) { readResponse ( $conn ); if ($response =~ /^$rsn OK/i) { last; } elsif ($response =~ /NO/) { 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; ++$lsn; undef @response; sendCommand ($conn, "$lsn LOGOUT"); while ( 1 ) { readResponse ($conn); if ( $response =~ /^$lsn 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 $user = shift; my $conn = shift; my @mbxs; my @mailboxes; # Get a list of the user's mailboxes # if ( $mbxList ) { Log("mbxList $mbxList"); # The user has supplied a list of mailboxes so only processes # the ones in that list @mbxs = split(/,/, $mbxList); foreach $mbx ( @mbxs ) { trim( *mbx ); push( @mailboxes, $mbx ); } return @mailboxes; } if ($debugMode) { Log("Get list of user's mailboxes",2); } sendCommand ($conn, "$rsn LIST \"\" *"); undef @response; while ( 1 ) { readResponse ($conn); if ( $response =~ /^$rsn OK/i ) { last; } elsif ( $response !~ /^\*/ ) { Log ("unexpected response: $response"); return 0; } } undef @mbxs; for $i (0 .. $#response) { # print STDERR "$response[$i]\n"; $response[$i] =~ s/\s+/ /; ($dmy,$mbx) = split(/"\/"/,$response[$i]); $mbx =~ s/^\s+//; $mbx =~ s/\s+$//; $mbx =~ s/"//g; if ($response[$i] =~ /NOSELECT/i) { if ($debugMode) { Log("$mbx is set NOSELECT,skip it",2); } next; } if (($mbx =~ /^\#/) && ($user ne 'anonymous')) { # Skip public mbxs unless we are migrating them next; } if ($mbx =~ /^\./) { # Skip mailboxes starting with a dot next; } push ( @mbxs, $mbx ) if $mbx ne ''; } if ( $mbxList ) { # The user has supplied a list of mailboxes so only processes # those @mbxs = split(/,/, $mbxList); } 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 $seen; my $empty; my $msgnum; my $from; my $flags; trim( *mailbox ); 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; } } sendCommand ( $conn, "1 FETCH 1:* (uid flags internaldate body[header.fields (From Date Message-ID)])"); undef @response; while ( 1 ) { readResponse ( $conn ); if ( $response =~ /^1 OK/i ) { # print STDERR "response $response\n"; last; } last if $response =~ /^1 NO|^1 BAD/; } @msgs = (); $flags = ''; for $i (0 .. $#response) { last if $response[$i] =~ /^1 OK FETCH complete/i; if ( $response[$i] =~ /^From: (.+)/ ) { $from = $1; } if ($response[$i] =~ /FLAGS/) { # Get the list of flags $response[$i] =~ /FLAGS \(([^\)]*)/; $flags = $1; $flags =~ s/\\Recent//; } if ( $response[$i] =~ /^Message-ID:\s*\<(.+)\>/i ) { $msgid = $1; } if ( $response[$i] =~ /INTERNALDATE/) { $response[$i] =~ /INTERNALDATE (.+) BODY/; # $response[$i] =~ /INTERNALDATE "(.+)" BODY/; $date = $1; $date =~ /"(.+)"/; $date = $1; $date =~ s/"//g; } # if ( $response[$i] =~ /\* (.+) [^FETCH]/ ) { if ( $response[$i] =~ /\* (.+) FETCH/ ) { ($msgnum) = split(/\s+/, $1); } if ( $msgnum and $from and $date and $msgid ) { push (@$msgs,"$msgid||||||$mailbox||||||$msgnum||||||$flags||||||$date"); $msgnum=$from=$msgid=$date=''; } } } sub fetchMsg { my $msgnum = shift; my $mbx = shift; my $conn = shift; my $message; Log(" Fetching msg $msgnum...") if $debug; ### sendCommand ($conn, "$rsn SELECT \"$mbx\""); sendCommand ($conn, "$rsn EXAMINE \"$mbx\""); while (1) { readResponse ($conn); last if ( $response =~ /^$rsn OK/i ); } sendCommand( $conn, "$rsn FETCH $msgnum (rfc822)"); while (1) { readResponse ($conn); if ( $response =~ /^$rsn 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"); return 0; } $message .= $segment; $cc += $n; } } } return $message; } sub usage { print STDOUT "usage:\n"; print STDOUT " imapsync -S sourceHost/sourceUser/sourcePassword\n"; print STDOUT " -D destHost/destUser/destPassword\n"; print STDOUT " -d debug\n"; print STDOUT " -L logfile\n"; print STDOUT " -m mailbox list (eg \"Inbox, Drafts, Notes\". Default is all mailboxes)\n"; exit; } sub processArgs { if ( !getopts( "dS:D:L:m:hI" ) ) { usage(); } ($sourceHost,$sourceUser,$sourcePwd) = split(/\//, $opt_S); ($destHost, $destUser, $destPwd) = split(/\//, $opt_D); $mbxList = $opt_m; $logfile = $opt_L; $debug = 1 if $opt_d; $showIMAP = 1 if $opt_I; usage() if $opt_h; } sub findMsg { my $conn = shift; my $msgid = shift; my $mbx = shift; my $msgnum; Log(" SELECT $mbx") if $debug; sendCommand ( $conn, "1 SELECT \"$mbx\""); while (1) { readResponse ($conn); last if $response =~ /^1 OK/; } Log(" Search for $msgid") if $debug; sendCommand ( $conn, "$rsn SEARCH header Message-Id \"$msgid\""); while (1) { readResponse ($conn); if ( $response =~ /\* SEARCH /i ) { ($dmy, $msgnum) = split(/\* SEARCH /i, $response); ($msgnum) = split(/ /, $msgnum); } last if $response =~ /^1 OK/; last if $response =~ /complete/i; } return $msgnum; } sub deleteMsg { my $conn = shift; my $msgid = shift; my $mbx = shift; my $rc; $msgnum = findMsg( $conn, $msgid, $mbx ); Log(" msgnum is $msgnum") if $debug; sendCommand ( $conn, "1 STORE $msgnum +FLAGS (\\Deleted)"); while (1) { readResponse ($conn); if ( $response =~ /^1 OK/i ) { $rc = 1; Log(" Marked $msgid for delete"); 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 $conn = shift; my $mbx = shift; Log("SELECT $mbx") if $debug; sendCommand ( $conn, "1 SELECT \"$mbx\""); while (1) { readResponse ($conn); last if $response =~ /^1 OK/; if ( $response =~ /^1 NO|^1 BAD/i ) { Log("Error selecting mailbox $mbx: $response"); last; } } sendCommand ( $conn, "1 EXPUNGE"); while (1) { readResponse ($conn); last if $response =~ /^1 OK/; if ( $response =~ /^1 BAD|^1 NO/i ) { print "Error expunging messages: $response\n"; last; } } } sub checkForAdds { my $added=0; Log("Checking for messages to add to $destHost/$destUser"); foreach $key ( @sourcekeys ) { if ( $destList{"$key"} eq '' ) { $entry = $sourceList{"$key"}; ($msgid,$mbx) = split(/\|\|\|\|\|\|/, $key); ($msgnum,$flags,$date) = split(/\|\|\|\|\|\|/, $entry); Log(" Adding $msgid to $mbx"); # Need to add this message to the dest host $message = fetchMsg( $msgnum, $mbx, $src ); insertMsg( $dst, $mbx, *message, $flags, $date ); $added++; } } return $added; } sub checkForUpdates { my $updated=0; # Compare the flags for the message on the source with the # one on the dest. Update the dest flags if they are different Log("Checking for flag changes to $destHost/$destUser"); foreach $key ( @sourcekeys ) { $entry = $sourceList{"$key"}; ($msgid,$mbx) = split(/\|\|\|\|\|\|/, $key); ($msgnum,$srcflags,$date) = split(/\|\|\|\|\|\|/, $entry); if ( $destList{"$key"} ne '' ) { $entry = $destList{"$key"}; ($msgid,$mbx) = split(/\|\|\|\|\|\|/, $key); ($msgnum,$dstflags,$date) = split(/\|\|\|\|\|\|/, $entry); $srcflags =~ s/\\Recent//i; $destflags =~ s/\\Recent//i; if ( $srcflags ne $dstflags ) { Log(" Need to update the flags for $msgid") if $debug; $updated++ if updateFlags( $dst, $msgid, $mbx, $srcflags, $dstflags ); } } } return $updated; } sub checkForDeletes { my $deleted=0; # Find any messages in the dest which are not in the source # and remove them from the dest Log("Checking for messages to remove from $destHost/$destUser"); foreach $key ( @destkeys) { if ( $sourceList{"$key"} eq '' ) { ($msgid, $mbx) = split(/\|\|\|\|\|\|/, $key); Log(" Need to delete $msgid from $mbx") if $debug; if ( deleteMsg( $dst, $msgid, $mbx ) ) { # Need to expunge messages from this mailbox when we're done $deleted++; push( @purgeMbxs, $mbx ); } } } # Now purge the messages we set to \Deleted foreach $mbx ( @purgeMbxs ) { expungeMbx( $dst, $mbx ); } return $deleted; } sub updateFlags { my $conn = shift; my $msgid = shift; my $mbx = shift; my $srcflags = shift; my $dstflags = shift; my $rc; Log("Searching for msgid $msgid in $mbx mailbox") if $debug; $msgnum = findMsg( $conn, $msgid, $mbx ); Log("Found msgnum $msgnum") if $debug; # Clear the dst flags then set the src flags sendCommand ( $conn, "1 STORE $msgnum -FLAGS ($dstflags)"); while (1) { readResponse ($conn); if ( $response =~ /^1 OK/i ) { Log(" Updated flags for $msgid"); $rc = 1; last; } if ( $response =~ /^1 BAD|^1 NO/i ) { Log("Error setting flags for $msgid: $response"); $rc = 0; last; } } sendCommand ( $conn, "1 STORE $msgnum +FLAGS ($srcflags)"); while (1) { readResponse ($conn); if ( $response =~ /^1 OK/i ) { Log(" Updated flags for $msgid"); $rc = 1; last; } if ( $response =~ /^1 BAD|^1 NO/i ) { Log("Error setting flags for $msgid: $response"); $rc = 0; last; } } return $rc; } sub namespace { my $conn = shift; my $prefix = shift; my $delimiter = shift; # Query the server with NAMESPACE so we can determine its # mailbox prefix (if any) and hierachy delimiter. @response = (); sendCommand( $conn, "1 NAMESPACE"); while ( 1 ) { readResponse( $conn ); if ( $response =~ /^1 OK/i ) { last; } elsif ( $response =~ /NO|BAD/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; last; } last if /^NO|^BAD/; } 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; # Adjust the mailbox name if the source and destination server # have different mailbox prefixes or hierarchy delimiters. if ( $debug ) { Log("src mbx $srcmbx"); Log("src prefix $srcPrefix"); Log("src delim $srcDelim"); Log("dst prefix $dstPrefix"); Log("dst delim $dstDelim"); } $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'; } $dstmbx =~ s#^$dstDelim##; } return $dstmbx; } sub flags { my $flags = shift; my @newflags; my $newflags; # Make sure the flags list contains only standard # IMAP flags. return unless $flags; $flags =~ s/\\Recent//i; foreach $_ ( split(/\s+/, $flags) ) { next unless substr($_,0,1) eq '\\'; push( @newflags, $_ ); } $newflags = join( ' ', @newflags ); $newflags =~ s/\\Deleted//ig if $opt_r; $newflags =~ s/^\s+|\s+$//g; return $newflags; }