imapcopy.pl 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. #!/usr/bin/perl
  2. # $Header: /mhub4/sources/imap-tools/imapcopy.pl,v 1.45 2012/03/15 12:35:11 rick Exp $
  3. #######################################################################
  4. # Program name imapcopy.pl #
  5. # Written by Rick Sanders #
  6. # #
  7. # Description #
  8. # #
  9. # imapcopy is a utility for copying a user's messages from one #
  10. # IMAP server to another. #
  11. # #
  12. # imapcopy is called like this: #
  13. # ./imapcopy -S host1/user1/password1 -D host2/user2/password2 #
  14. # #
  15. # Optional arguments: #
  16. # -d debug #
  17. # -I show IMAP protocol exchanges #
  18. # -L logfile #
  19. # -m mailbox list (copy only certain mailboxes,see usage notes) #
  20. # -r reset the \DELETE flag on copied messages #
  21. # -p <root mailbox> put copied mailboxes under a root mbx #
  22. # -M <file> mailbox mapping (eg, src:inbox -> dst:inbox_copied) #
  23. # -i initialize mailbox (remove existing msgs first) #
  24. # -U run in "update" mode
  25. # Run imapcopy.pl -h to see complete set of arguments. #
  26. #######################################################################
  27. use Socket;
  28. use FileHandle;
  29. use Fcntl;
  30. use Getopt::Std;
  31. use IO::Socket;
  32. #################################################################
  33. # Main program. #
  34. #################################################################
  35. init();
  36. # Get list of all messages on the source host
  37. #
  38. connectToHost($sourceHost, \$src) or exit;
  39. login($sourceUser,$sourcePwd, $sourceHost, $src, $srcMethod) or exit;
  40. namespace( $src, \$srcPrefix, \$srcDelim, $opt_x );
  41. connectToHost( $destHost, \$dst ) or exit;
  42. login( $destUser,$destPwd, $destHost, $dst, $dstMethod ) or exit;
  43. namespace( $dst, \$dstPrefix, \$dstDelim, $opt_y );
  44. @mbxs = getMailboxList( $srcPrefix, $src );
  45. # Exclude certain mbxs if that's what the user wants
  46. exclude_mbxs( \@mbxs ) if $excludeMbxs;
  47. map_mbx_names( \%mbx_map, $srcDelim, $dstDelim );
  48. if ( $archive_mbx ) {
  49. # Create an archive mbx on the source to receive copies of messsages
  50. createMbx( $archive_mbx, $src ) unless mbxExists( $archive_mbx, $src);
  51. }
  52. $total=$mbxs_processed = 0;
  53. my $delete_msg_list;
  54. $num_mbxs = $#mbxs + 1;
  55. Log("Number of mailboxes to process: $num_mbxs");
  56. foreach $srcmbx ( @mbxs ) {
  57. ### encode( \$srcmbx );
  58. next if $srcmbx eq $archive_mbx;
  59. $archived=0;
  60. $mbxs_processed++;
  61. if ( $verbose ) {
  62. $line = "Processing $srcmbx " . '(' . $mbxs_processed . '/' . $num_mbxs . ')';
  63. Log("$line");
  64. }
  65. $dstmbx = mailboxName( $srcmbx,$srcPrefix,$srcDelim,$dstPrefix,$dstDelim );
  66. $LAST = "$dstmbx";
  67. createMbx( $dstmbx, $dst ) unless mbxExists( $dstmbx, $dst);
  68. if ( $include_nosel_mbxs ) {
  69. # If a mailbox was 'Noselect' on the src but the user wants
  70. # it created as a regular folder on the dst then do so. They
  71. # don't hold any messages so after creating them we don't need
  72. # to do anything else.
  73. next if $nosel_mbxs{"$srcmbx"};
  74. }
  75. selectMbx( $dstmbx, $dst );
  76. if ( $update ) {
  77. getMsgIdList( $dstmbx, \%DST_MSGS, $dst );
  78. }
  79. init_mbx( $dstmbx, $dst ) if $init_mbx;
  80. $checkpoint = "$srcmbx|$sourceHost|$sourceUser|$sourcePwd|";
  81. $checkpoint .= "$destHost|$destUser|$destPwd";
  82. if ( $sent_after ) {
  83. getDatedMsgList( $srcmbx, $sent_after, \@msgs, $src, 'EXAMINE' );
  84. } else {
  85. getMsgList( $srcmbx, \@msgs, $src, 'EXAMINE' );
  86. }
  87. my $msgcount = $#msgs + 1;
  88. Log(" Copying $msgcount messages in $srcmbx mailbox") if $verbose;
  89. if ( $msgcount == 0 ) {
  90. Log(" $srcmbx mailbox is empty");
  91. next;
  92. }
  93. $copied=0;
  94. foreach $_ ( @msgs ) {
  95. alarm $timeout;
  96. ($msgnum,$date,$flags,$msgid) = split(/\|/, $_);
  97. Log("msgnum=$msgnum,msgid=$msgid") if $debug;
  98. if ( $update ) {
  99. # Don't insert the message if it already exists
  100. next if $DST_MSGS{"$msgid"};
  101. }
  102. # Strip off TZ offset if it exists
  103. $date =~ s/\((.+)\)$//;
  104. $date =~ s/\s+$//g;
  105. $LAST = "$dstmbx|$msgnum";
  106. next unless fetchMsg( $msgnum, \$message, $srcmbx, $src );
  107. # $message =~ /Message-Id: (.+)/i;
  108. # Log("message has msgid = $1");
  109. alarm 0;
  110. if ( $conn_timed_out ) {
  111. Log("$sourceHost timed out");
  112. reconnect( $checkpoint, $src );
  113. $conn_timed_out = 0;
  114. next;
  115. }
  116. next unless $message;
  117. alarm $timeout;
  118. $copied++ if insertMsg( $dst, $dstmbx, *message, $flags, $date );
  119. if ( $archive_mbx ) {
  120. # Put a copy of the message in it too
  121. if ( insertMsg( $src, $archive_mbx, *message, $flags, $date ) ) {
  122. $archived++;
  123. if ( $rem_src_msgs ) {
  124. $delete_msg_list .= "$msgnum ";
  125. }
  126. }
  127. }
  128. if ( $copied/100 == int($copied/100)) {
  129. Log(" Copied $copied messages so far") if $verbose;
  130. }
  131. alarm 0;
  132. if ( $conn_timed_out ) {
  133. Log("$destHost timed out");
  134. reconnect( $checkpoint, $dst );
  135. $conn_timed_out = 0;
  136. next;
  137. }
  138. }
  139. $total += $copied;
  140. if ( $use_utf7 ) {
  141. $dstmbx = Unicode::IMAPUtf7::imap_utf7_decode( $dstmbx );
  142. }
  143. if ( $verbose ) {
  144. $line = " Copied $copied messages to $dstmbx ";
  145. $line .= '(' . $mbxs_processed . '/' . $num_mbxs . ')';
  146. Log( "$line ");
  147. } else {
  148. Log(" Copied $copied messages to $dstmbx");
  149. }
  150. if ( $archive_mbx ) {
  151. Log(" Copied $archived messages to $archive_mbx mailbox");
  152. if ( $rem_src_msgs ) {
  153. # Remove the messages from the source mailbox
  154. Log("Removing messages from $srcmbx on source");
  155. delete_msg_list( $delete_msg_list, $srcmbx, $src );
  156. expungeMbx( $srcmbx, $src );
  157. }
  158. }
  159. }
  160. Log("Copied $total total messages");
  161. logout( $src );
  162. logout( $dst );
  163. exit;
  164. sub init {
  165. $os = $ENV{'OS'};
  166. processArgs();
  167. # Open the logFile
  168. #
  169. if ( $logfile ) {
  170. if ( !open(LOG, ">> $logfile")) {
  171. print STDOUT "Can't open $logfile: $!\n";
  172. exit;
  173. }
  174. select(LOG); $| = 1;
  175. }
  176. Log("$0 starting");
  177. # Determine whether we have SSL support via openSSL and IO::Socket::SSL
  178. $ssl_installed = 1;
  179. eval 'use IO::Socket::SSL';
  180. if ( $@ ) {
  181. $ssl_installed = 0;
  182. }
  183. # Set up signal handling
  184. $SIG{'ALRM'} = 'signalHandler';
  185. $SIG{'HUP'} = 'signalHandler';
  186. $SIG{'INT'} = 'signalHandler';
  187. $SIG{'TERM'} = 'signalHandler';
  188. $SIG{'URG'} = 'signalHandler';
  189. }
  190. #
  191. # sendCommand
  192. #
  193. # This subroutine formats and sends an IMAP protocol command to an
  194. # IMAP server on a specified connection.
  195. #
  196. sub sendCommand {
  197. my $fd = shift;
  198. my $cmd = shift;
  199. print $fd "$cmd\r\n";
  200. Log (">> $cmd") if $showIMAP;
  201. }
  202. #
  203. # readResponse
  204. #
  205. # This subroutine reads and formats an IMAP protocol response from an
  206. # IMAP server on a specified connection.
  207. #
  208. sub readResponse {
  209. my $fd = shift;
  210. $response = <$fd>;
  211. chop $response;
  212. $response =~ s/\r//g;
  213. push (@response,$response);
  214. Log ("<< $response") if $showIMAP;
  215. }
  216. #
  217. # Log
  218. #
  219. # This subroutine formats and writes a log message to STDERR.
  220. #
  221. sub Log {
  222. my $str = shift;
  223. # If a logfile has been specified then write the output to it
  224. # Otherwise write it to STDOUT
  225. if ( $logfile ) {
  226. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
  227. if ($year < 99) { $yr = 2000; }
  228. else { $yr = 1900; }
  229. $line = sprintf ("%.2d-%.2d-%d.%.2d:%.2d:%.2d %s\n",
  230. $mon + 1, $mday, $year + $yr, $hour, $min, $sec,$str);
  231. print LOG "$line";
  232. }
  233. print STDOUT "$str\n" unless $quiet_mode;
  234. }
  235. sub createMbx {
  236. my $mbx = shift;
  237. my $conn = shift;
  238. # Create the mailbox if necessary
  239. sendCommand ($conn, "1 CREATE \"$mbx\"");
  240. while ( 1 ) {
  241. readResponse ($conn);
  242. last if $response =~ /^1 OK/i;
  243. last if $response =~ /already exists/i;
  244. if ( $response =~ /^1 NO|^1 BAD|^\* BYE/ ) {
  245. Log ("Error creating $mbx: $response");
  246. last;
  247. }
  248. if ( $response eq '' or $response =~ /^1 NO/ ) {
  249. Log ("unexpected CREATE response: >$response<");
  250. Log("response is NULL");
  251. resume();
  252. last;
  253. }
  254. }
  255. }
  256. # insertMsg
  257. #
  258. # This routine inserts a message into a user's mailbox
  259. #
  260. sub insertMsg {
  261. local ($conn, $mbx, *message, $flags, $date) = @_;
  262. local ($lenx);
  263. $lenx = length($message);
  264. Log(" Inserting message") if $debug;
  265. my $mb = $lenx/1000000;
  266. if ( $max_size and $mb > $max_size ) {
  267. commafy( \$lenx );
  268. Log(" Skipping message because its size ($lenx) exceeds the $max_size MB limit");
  269. return;
  270. }
  271. $totalBytes = $totalBytes + $lenx;
  272. $totalMsgs++;
  273. $flags = flags( $flags );
  274. fixup_date( \$date );
  275. sendCommand ($conn, "1 APPEND \"$mbx\" ($flags) \"$date\" \{$lenx\}");
  276. readResponse ($conn);
  277. if ( $response !~ /^\+/ ) {
  278. Log ("1 unexpected APPEND response: >$response<");
  279. if ( $response eq '' or $response =~ /^1 NO/ ) {
  280. Log("response is NULL");
  281. resume();
  282. next;
  283. }
  284. # next;
  285. push(@errors,"Error appending message to $mbx for $user");
  286. return 0;
  287. }
  288. print $conn "$message\r\n";
  289. undef @response;
  290. while ( 1 ) {
  291. readResponse ($conn);
  292. if ( $response =~ /^1 OK/i ) {
  293. last;
  294. }
  295. elsif ( $response !~ /^\*/ ) {
  296. Log ("unexpected APPEND response: $response");
  297. # next;
  298. return 0;
  299. }
  300. }
  301. return 1;
  302. }
  303. # Make a connection to a IMAP host
  304. sub connectToHost {
  305. my $host = shift;
  306. my $conn = shift;
  307. Log("Connecting to $host") if $debug;
  308. ($host,$port) = split(/:/, $host);
  309. $port = 143 unless $port;
  310. # We know whether to use SSL for ports 143 and 993. For any
  311. # other ones we'll have to figure it out.
  312. $mode = sslmode( $host, $port );
  313. if ( $mode eq 'SSL' ) {
  314. unless( $ssl_installed == 1 ) {
  315. warn("You must have openSSL and IO::Socket::SSL installed to use an SSL connection");
  316. Log("You must have openSSL and IO::Socket::SSL installed to use an SSL connection");
  317. exit;
  318. }
  319. Log("Attempting an SSL connection") if $debug;
  320. $$conn = IO::Socket::SSL->new(
  321. Proto => "tcp",
  322. SSL_verify_mode => 0x00,
  323. PeerAddr => $host,
  324. PeerPort => $port,
  325. );
  326. unless ( $$conn ) {
  327. $error = IO::Socket::SSL::errstr();
  328. Log("Error connecting to $host: $error");
  329. exit;
  330. }
  331. } else {
  332. # Non-SSL connection
  333. Log("Attempting a non-SSL connection") if $debug;
  334. $$conn = IO::Socket::INET->new(
  335. Proto => "tcp",
  336. PeerAddr => $host,
  337. PeerPort => $port,
  338. );
  339. unless ( $$conn ) {
  340. Log("Error connecting to $host:$port: $@");
  341. warn "Error connecting to $host:$port: $@";
  342. exit;
  343. }
  344. }
  345. Log("Connected to $host on port $port");
  346. }
  347. sub sslmode {
  348. my $host = shift;
  349. my $port = shift;
  350. my $mode;
  351. # Determine whether to make an SSL connection
  352. # to the host. Return 'SSL' if so.
  353. if ( $port == 143 ) {
  354. # Standard non-SSL port
  355. return '';
  356. } elsif ( $port == 993 ) {
  357. # Standard SSL port
  358. return 'SSL';
  359. }
  360. unless ( $ssl_installed ) {
  361. # We don't have SSL installed on this machine
  362. return '';
  363. }
  364. # For any other port we need to determine whether it supports SSL
  365. my $conn = IO::Socket::SSL->new(
  366. Proto => "tcp",
  367. SSL_verify_mode => 0x00,
  368. PeerAddr => $host,
  369. PeerPort => $port,
  370. );
  371. if ( $conn ) {
  372. close( $conn );
  373. $mode = 'SSL';
  374. } else {
  375. $mode = '';
  376. }
  377. return $mode;
  378. }
  379. # trim
  380. #
  381. # remove leading and trailing spaces from a string
  382. sub trim {
  383. local (*string) = @_;
  384. $string =~ s/^\s+//;
  385. $string =~ s/\s+$//;
  386. return;
  387. }
  388. # login
  389. #
  390. # login in at the source host with the user's name and password
  391. #
  392. sub login {
  393. my $user = shift;
  394. my $pwd = shift;
  395. my $host = shift;
  396. my $conn = shift;
  397. my $method = shift;
  398. Log("Authenticating to $host as $user");
  399. if ( uc( $method ) eq 'CRAM-MD5' ) {
  400. # A CRAM-MD5 login is requested
  401. Log("login method $method");
  402. my $rc = login_cram_md5( $user, $pwd, $conn );
  403. return $rc;
  404. }
  405. # Otherwise do a PLAIN login
  406. sendCommand ($conn, "1 LOGIN $user \"$pwd\"");
  407. while (1) {
  408. readResponse ( $conn );
  409. last if $response =~ /^1 OK/i;
  410. if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
  411. Log ("unexpected LOGIN response: $response");
  412. return 0;
  413. }
  414. }
  415. Log("Logged in as $user") if $debug;
  416. return 1;
  417. }
  418. sub login_cram_md5 {
  419. my $user = shift;
  420. my $pwd = shift;
  421. my $conn = shift;
  422. sendCommand ($conn, "1 AUTHENTICATE CRAM-MD5");
  423. while (1) {
  424. readResponse ( $conn );
  425. last if $response =~ /^\+/;
  426. if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
  427. Log ("unexpected LOGIN response: $response");
  428. return 0;
  429. }
  430. }
  431. my ($challenge) = $response =~ /^\+ (.+)/;
  432. Log("challenge $challenge") if $debug;
  433. $response = cram_md5( $challenge, $user, $pwd );
  434. Log("response $response") if $debug;
  435. sendCommand ($conn, $response);
  436. while (1) {
  437. readResponse ( $conn );
  438. last if $response =~ /^1 OK/i;
  439. if ($response =~ /^1 NO|^1 BAD|^\* BYE/i) {
  440. Log ("unexpected LOGIN response: $response");
  441. return 0;
  442. }
  443. }
  444. Log("Logged in as $user") if $debug;
  445. return 1;
  446. }
  447. # logout
  448. #
  449. # log out from the host
  450. #
  451. sub logout {
  452. my $conn = shift;
  453. undef @response;
  454. sendCommand ($conn, "1 LOGOUT");
  455. while ( 1 ) {
  456. readResponse ($conn);
  457. if ( $response =~ /^1 OK/i ) {
  458. last;
  459. }
  460. elsif ( $response !~ /^\*/ ) {
  461. Log ("unexpected LOGOUT response: $response");
  462. last;
  463. }
  464. }
  465. close $conn;
  466. return;
  467. }
  468. # getMailboxList
  469. #
  470. # get a list of the user's mailboxes from the source host
  471. #
  472. sub getMailboxList {
  473. my $prefix = shift;
  474. my $conn = shift;
  475. my @mbxs;
  476. # Get a list of the user's mailboxes
  477. #
  478. Log("Get list of user's mailboxes",2) if $debugMode;
  479. if ( $mbxList ) {
  480. foreach $mbx ( split(/,/, $mbxList) ) {
  481. $mbx = $prefix . $mbx if $prefix;
  482. if ( $opt_R ) {
  483. # Get all submailboxes under the ones specified
  484. $mbx .= '*';
  485. @mailboxes = listMailboxes( $mbx, $conn);
  486. push( @mbxs, @mailboxes );
  487. } else {
  488. push( @mbxs, $mbx );
  489. }
  490. }
  491. } else {
  492. # Get all mailboxes
  493. @mbxs = listMailboxes( '*', $conn);
  494. }
  495. return @mbxs;
  496. }
  497. # exclude_mbxs
  498. #
  499. # Exclude certain mailboxes from the list if the user
  500. # has provided an exclude list with the -e argument
  501. sub exclude_mbxs {
  502. my $mbxs = shift;
  503. my @new_list;
  504. my %exclude;
  505. foreach my $exclude ( split(/,/, $excludeMbxs ) ) {
  506. $exclude{"$exclude"} = 1;
  507. }
  508. foreach my $mbx ( @$mbxs ) {
  509. next if $exclude{"$mbx"};
  510. push( @new_list, $mbx );
  511. }
  512. @$mbxs = @new_list;
  513. }
  514. # listMailboxes
  515. #
  516. # Get a list of the user's mailboxes
  517. #
  518. sub listMailboxes {
  519. my $mbx = shift;
  520. my $conn = shift;
  521. sendCommand ($conn, "1 LIST \"\" \"$mbx\"");
  522. undef @response;
  523. while ( 1 ) {
  524. &readResponse ($conn);
  525. if ( $response =~ /^1 OK/i ) {
  526. last;
  527. }
  528. elsif ( $response !~ /^\*/ ) {
  529. &Log ("unexpected response: $response");
  530. return 0;
  531. }
  532. }
  533. @mbxs = ();
  534. for $i (0 .. $#response) {
  535. $response[$i] =~ s/\s+/ /;
  536. if ( $response[$i] =~ /"$/ ) {
  537. $response[$i] =~ /\* LIST \((.*)\) "(.+)" "(.+)"/i;
  538. $mbx = $3;
  539. } elsif ( $response[$i] =~ /\* LIST \((.*)\) NIL (.+)/i ) {
  540. $mbx = $2;
  541. } else {
  542. $response[$i] =~ /\* LIST \((.*)\) "(.+)" (.+)/i;
  543. $mbx = $3;
  544. }
  545. $mbx =~ s/^\s+//; $mbx =~ s/\s+$//;
  546. if ($response[$i] =~ /NOSELECT/i) {
  547. if ( $include_nosel_mbxs ) {
  548. $nosel_mbxs{"$mbx"} = 1;
  549. } else {
  550. Log("$mbx is set NOSELECT, skipping it") if $debug;
  551. next;
  552. }
  553. }
  554. if ($mbx =~ /^\./) {
  555. # Skip mailboxes starting with a dot
  556. next;
  557. }
  558. push ( @mbxs, $mbx ) if $mbx ne '';
  559. }
  560. return @mbxs;
  561. }
  562. # getMsgList
  563. #
  564. # Get a list of the user's messages in the indicated mailbox on
  565. # the source host
  566. #
  567. sub getMsgList {
  568. my $mailbox = shift;
  569. my $msgs = shift;
  570. my $conn = shift;
  571. my $mode = shift;
  572. my $seen;
  573. my $empty;
  574. my $msgnum;
  575. my $from;
  576. my $flags;
  577. my $msgid;
  578. @$msgs = ();
  579. $mode = 'EXAMINE' unless $mode;
  580. sendCommand ($conn, "1 $mode \"$mailbox\"");
  581. undef @response;
  582. $empty=0;
  583. while ( 1 ) {
  584. readResponse ( $conn );
  585. if ( $response =~ / 0 EXISTS/i ) { $empty=1; }
  586. if ( $response =~ /^1 OK/i ) {
  587. last;
  588. }
  589. elsif ( $response !~ /^\*/ ) {
  590. Log ("unexpected response: $response");
  591. return 0;
  592. }
  593. }
  594. return 1 if $empty;
  595. my $start = 1;
  596. my $end = '*';
  597. $start = $start_fetch if $start_fetch;
  598. $end = $end_fetch if $end_fetch;
  599. sendCommand ( $conn, "1 FETCH $start:$end (uid flags internaldate body[header.fields (From Date Message-Id)])");
  600. @response = ();
  601. while ( 1 ) {
  602. readResponse ( $conn );
  603. if ( $response =~ /^1 OK/i ) {
  604. last;
  605. }
  606. last if $response =~ /^1 NO|^1 BAD|^\* BYE/;
  607. }
  608. $flags = '';
  609. for $i (0 .. $#response) {
  610. last if $response[$i] =~ /^1 OK FETCH complete/i;
  611. if ($response[$i] =~ /FLAGS/) {
  612. # Get the list of flags
  613. $response[$i] =~ /FLAGS \(([^\)]*)/;
  614. $flags = $1;
  615. $flags =~ s/\\Recent//;
  616. }
  617. if ( $response[$i] =~ /INTERNALDATE/) {
  618. $response[$i] =~ /INTERNALDATE (.+) BODY/i;
  619. # $response[$i] =~ /INTERNALDATE "(.+)" BODY/;
  620. $date = $1;
  621. $date =~ /"(.+)"/;
  622. $date = $1;
  623. $date =~ s/"//g;
  624. }
  625. if ( $response[$i] =~ /^Message-Id: (.+)/i ) {
  626. $msgid = $1;
  627. }
  628. # if ( $response[$i] =~ /\* (.+) [^FETCH]/ ) {
  629. if ( $response[$i] =~ /\* (.+) FETCH/ ) {
  630. ($msgnum) = split(/\s+/, $1);
  631. }
  632. if ( $msgnum and $date and $msgid ) {
  633. # if ( $msgnum and $date ) {
  634. push (@$msgs,"$msgnum|$date|$flags|$msgid");
  635. $msgnum = $date = $msgid = '';
  636. }
  637. }
  638. return 1;
  639. }
  640. # getDatedMsgList
  641. #
  642. # Get a list of the user's messages in a mailbox on
  643. # the host which were sent after the specified date
  644. #
  645. sub getDatedMsgList {
  646. my $mailbox = shift;
  647. my $cutoff_date = shift;
  648. my $msgs = shift;
  649. my $conn = shift;
  650. my $oper = shift;
  651. my ($seen, $empty, @list,$msgid);
  652. # Get a list of messages sent after the specified date
  653. Log("Searching for messages after $cutoff_date");
  654. @list = ();
  655. @$msgs = ();
  656. sendCommand ($conn, "1 $oper \"$mailbox\"");
  657. while ( 1 ) {
  658. readResponse ($conn);
  659. if ( $response =~ / EXISTS/i) {
  660. $response =~ /\* ([^EXISTS]*)/;
  661. # Log(" There are $1 messages in $mailbox");
  662. } elsif ( $response =~ /^1 OK/i ) {
  663. last;
  664. } elsif ( $response =~ /^1 NO/i ) {
  665. Log ("unexpected SELECT response: $response");
  666. return 0;
  667. } elsif ( $response !~ /^\*/ ) {
  668. Log ("unexpected SELECT response: $response");
  669. return 0;
  670. }
  671. }
  672. my ($date,$ts) = split(/\s+/, $cutoff_date);
  673. #
  674. # Get list of messages sent before the reference date
  675. #
  676. Log("Get messages sent after $date") if $debug;
  677. $nums = "";
  678. sendCommand ($conn, "1 SEARCH SINCE \"$date\"");
  679. while ( 1 ) {
  680. readResponse ($conn);
  681. if ( $response =~ /^1 OK/i ) {
  682. last;
  683. }
  684. elsif ( $response =~ /^\*\s+SEARCH/i ) {
  685. ($nums) = ($response =~ /^\*\s+SEARCH\s+(.*)/i);
  686. }
  687. elsif ( $response !~ /^\*/ ) {
  688. Log ("unexpected SEARCH response: $response");
  689. return;
  690. }
  691. }
  692. Log("$nums") if $debug;
  693. if ( $nums eq "" ) {
  694. Log (" $mailbox has no messages sent before $date") if $debug;
  695. return;
  696. }
  697. my @number = split(/\s+/, $nums);
  698. $n = $#number + 1;
  699. $nums =~ s/\s+/ /g;
  700. @msgList = ();
  701. @msgList = split(/ /, $nums);
  702. if ($#msgList == -1) {
  703. # No msgs in this mailbox
  704. return 1;
  705. }
  706. @$msgs = ();
  707. for $num (@msgList) {
  708. sendCommand ( $conn, "1 FETCH $num (uid flags internaldate body[header.fields (Message-Id Date)])");
  709. undef @response;
  710. while ( 1 ) {
  711. readResponse ( $conn );
  712. Log("RESP1 $response");
  713. if ( $response =~ /^1 OK/i ) {
  714. last;
  715. }
  716. last if $response =~ /^1 NO|^1 BAD|^\* BYE/;
  717. }
  718. $flags = '';
  719. my $msgid;
  720. foreach $_ ( @response ) {
  721. last if /^1 OK FETCH complete/i;
  722. if ( /FLAGS/ ) {
  723. # Get the list of flags
  724. /FLAGS \(([^\)]*)/;
  725. $flags = $1;
  726. $flags =~ s/\\Recent//;
  727. }
  728. if ( /Message-Id:\s*(.+)/i ) {
  729. $msgid = $1;
  730. }
  731. if ( /INTERNALDATE/) {
  732. /INTERNALDATE (.+) BODY/i;
  733. $date = $1;
  734. $date =~ /"(.+)"/;
  735. $date = $1;
  736. $date =~ s/"//g;
  737. #### next if check_cutoff_date( $date, $cutoff_date );
  738. }
  739. if ( /\* (.+) FETCH/ ) {
  740. ($msgnum) = split(/\s+/, $1);
  741. }
  742. if ( $msgnum and $date ) {
  743. push (@$msgs,"$msgnum|$date|$flags|$msgid");
  744. $msgnum=$msgid=$date=$flags='';
  745. }
  746. }
  747. }
  748. foreach $_ ( @$msgs ) {
  749. Log("getDated found $_") if $debug;
  750. }
  751. return 1;
  752. }
  753. sub mbxExists {
  754. my $mbx = shift;
  755. my $conn = shift;
  756. my $status = 1;
  757. # Determine whether a mailbox exists
  758. sendCommand ($conn, "1 EXAMINE \"$mbx\"");
  759. while (1) {
  760. readResponse ($conn);
  761. last if $response =~ /^1 OK/i;
  762. if ( $response =~ /^1 NO|^1 BAD|^\* BYE/ ) {
  763. $status = 0;
  764. last;
  765. }
  766. }
  767. return $status;
  768. }
  769. sub fetchMsg {
  770. my $msgnum = shift;
  771. my $message = shift;
  772. my $mbx = shift;
  773. my $conn = shift;
  774. Log(" Fetching msg $msgnum...") if $debug;
  775. $$message = '';
  776. sendCommand( $conn, "1 FETCH $msgnum (rfc822)");
  777. while (1) {
  778. readResponse ($conn);
  779. last if $response =~ /^1 NO|^1 BAD|^\* BYE/;
  780. if ( $response eq '' ) {
  781. Log("RESP2 >$response<");
  782. resume();
  783. return 0;
  784. }
  785. if ( $response =~ /^1 OK/i ) {
  786. $size = length($$message);
  787. last;
  788. }
  789. elsif ($response =~ /message number out of range/i) {
  790. Log ("Error fetching uid $uid: out of range",2);
  791. $stat=0;
  792. last;
  793. }
  794. elsif ($response =~ /Bogus sequence in FETCH/i) {
  795. Log ("Error fetching uid $uid: Bogus sequence in FETCH",2);
  796. $stat=0;
  797. last;
  798. }
  799. elsif ( $response =~ /message could not be processed/i ) {
  800. Log("Message could not be processed, skipping it ($user,msgnum $msgnum,$destMbx)");
  801. push(@errors,"Message could not be processed, skipping it ($user,msgnum $msgnum,$destMbx)");
  802. $stat=0;
  803. last;
  804. }
  805. elsif
  806. ($response =~ /^\*\s+$msgnum\s+FETCH\s+\(.*RFC822\s+\{[0-9]+\}/i) {
  807. ($len) = ($response =~ /^\*\s+$msgnum\s+FETCH\s+\(.*RFC822\s+\{([0-9]+)\}/i);
  808. $cc = 0;
  809. $$message = "";
  810. while ( $cc < $len ) {
  811. $n = 0;
  812. $n = read ($conn, $segment, $len - $cc);
  813. if ( $n == 0 ) {
  814. Log ("unable to read $len bytes");
  815. resume();
  816. return 0;
  817. }
  818. $$message .= $segment;
  819. $cc += $n;
  820. }
  821. }
  822. }
  823. return 1;
  824. }
  825. sub usage {
  826. print STDOUT "usage:\n";
  827. print STDOUT " imapcopy -S sourceHost/sourceUser/sourcePassword [/CRAM-MD5]\n";
  828. print STDOUT " -D destHost/destUser/destPassword [/CRAM-MD5]\n";
  829. print STDOUT " -d debug\n";
  830. print STDOUT " -I show IMAP protocol exchanges\n";
  831. print STDOUT " -L logfile\n";
  832. print STDOUT " -m mailbox list (eg \"Inbox, Drafts, Notes\". Default is all mailboxes)\n";
  833. print STDOUT " -R include submailboxes when used with -m\n\n";
  834. print STDOUT " -e exclude mailbox list\n";
  835. print STDOUT " -r remove msgs from source mbx after copying\n";
  836. print STDOUT " -p <mailbox> put copied mailboxes under a root mailbox\n";
  837. print STDOUT " -A <mailbox> copy to local mailbox from scrmbx\n";
  838. print STDOUT " -x <mbx delimiter [mbx prefix]> source (eg, -x '. INBOX.'\n";
  839. print STDOUT " -y <mbx delimiter [mbx prefix]> destination\n";
  840. print STDOUT " -i initialize mailbox (remove existing messages first\n";
  841. print STDOUT " -M <file> mailbox map file. Maps src mbxs to dst mbxs. ";
  842. print STDOUT "Each line in the file should be 'src mbx:dst mbx'\n";
  843. print STDOUT " -q quiet mode (still writes to the logfile)\n";
  844. print STDOUT " -t <timeout in seconds>\n";
  845. print STDOUT " -T copy custom flags (eg, \$Label1,\$MDNSent,etc)\n";
  846. print STDOUT " -a <DD-MMM-YYYY> copy only messages after this date\n";
  847. print STDOUT " -X <megabytes> Skip any message exceeding this size\n";
  848. print STDOUT " -U update mode, don't copy messages that already exist\n";
  849. print STDOUT " -B <msgnum> Starting point for message fetch\n";
  850. print STDOUT " -E <msgnum> Ending point for message fetch\n";
  851. exit;
  852. }
  853. sub processArgs {
  854. if ( !getopts( "dS:D:L:m:hIp:M:rqx:y:e:Rt:Tia:X:vP:A:UB:E:" ) ) {
  855. usage();
  856. }
  857. if ( $opt_S =~ /\\/ ) {
  858. ($sourceHost, $sourceUser, $sourcePwd,$srcMethod) = split(/\\/, $opt_S);
  859. } else {
  860. ($sourceHost, $sourceUser, $sourcePwd,$srcMethod) = split(/\//, $opt_S);
  861. }
  862. if ( $opt_D =~ /\\/ ) {
  863. ($destHost, $destUser, $destPwd,$dstMethod) = split(/\\/, $opt_D);
  864. } else {
  865. ($destHost, $destUser, $destPwd,$dstMethod) = split(/\//, $opt_D);
  866. }
  867. $mbxList = $opt_m;
  868. $logfile = $opt_L;
  869. $root_mbx = $opt_p;
  870. $timeout = $opt_t;
  871. $tags = $opt_T;
  872. $debug = 1 if $opt_d;
  873. $verbose = 1 if $opt_v;
  874. $showIMAP = 1 if $opt_I;
  875. $submbxs = 1 if $opt_R;
  876. $init_mbx = 1 if $opt_i;
  877. $quiet_mode = 1 if $opt_q;
  878. $update = 1 if $opt_U;
  879. $include_nosel_mbxs = 1 if $opt_s;
  880. $rem_src_msgs = 1 if $opt_r;
  881. $mbx_map_fn = $opt_M;
  882. $excludeMbxs = $opt_e;
  883. $sent_after = $opt_a;
  884. $max_size = $opt_X;
  885. $public_mbxs = $opt_P;
  886. $archive_mbx = $opt_A;
  887. $start_fetch = $opt_B;
  888. $end_fetch = $opt_E;
  889. $timeout = 300 unless $timeout;
  890. Log("Running in update mode") if $update;
  891. validate_date( $sent_after ) if $sent_after;
  892. usage() if $opt_h;
  893. }
  894. sub selectMbx {
  895. my $mbx = shift;
  896. my $conn = shift;
  897. # Some IMAP clients such as Outlook and Netscape) do not automatically list
  898. # all mailboxes. The user must manually subscribe to them. This routine
  899. # does that for the user by marking the mailbox as 'subscribed'.
  900. sendCommand( $conn, "1 SUBSCRIBE \"$mbx\"");
  901. while ( 1 ) {
  902. readResponse( $conn );
  903. if ( $response =~ /^1 OK/i ) {
  904. Log("Mailbox $mbx has been subscribed") if $debug;
  905. last;
  906. } elsif ( $response =~ /^1 NO|^1 BAD|\^* BYE/i ) {
  907. Log("Unexpected response to subscribe $mbx command: $response");
  908. last;
  909. }
  910. }
  911. # Now select the mailbox
  912. sendCommand( $conn, "1 SELECT \"$mbx\"");
  913. while ( 1 ) {
  914. readResponse( $conn );
  915. if ( $response =~ /^1 OK/i ) {
  916. last;
  917. } elsif ( $response =~ /^1 NO|^1 BAD|^\* BYE/i ) {
  918. Log("Unexpected response to SELECT $mbx command: $response");
  919. last;
  920. }
  921. }
  922. }
  923. sub namespace {
  924. my $conn = shift;
  925. my $prefix = shift;
  926. my $delimiter = shift;
  927. my $mbx_delim = shift;
  928. # Query the server with NAMESPACE so we can determine its
  929. # mailbox prefix (if any) and hierachy delimiter.
  930. if ( $mbx_delim ) {
  931. # The user has supplied a mbx delimiter and optionally a prefix.
  932. Log("Using user-supplied mailbox hierarchy delimiter $mbx_delim");
  933. ($$delimiter,$$prefix) = split(/\s+/, $mbx_delim);
  934. return;
  935. }
  936. @response = ();
  937. sendCommand( $conn, "1 NAMESPACE");
  938. while ( 1 ) {
  939. readResponse( $conn );
  940. if ( $response =~ /^1 OK/i ) {
  941. last;
  942. } elsif ( $response =~ /^1 NO|^1 BAD|^\* BYE/i ) {
  943. Log("Unexpected response to NAMESPACE command: $response");
  944. last;
  945. }
  946. }
  947. foreach $_ ( @response ) {
  948. if ( /NAMESPACE/i ) {
  949. my $i = index( $_, '((' );
  950. my $j = index( $_, '))' );
  951. my $val = substr($_,$i+2,$j-$i-3);
  952. ($val) = split(/\)/, $val);
  953. ($$prefix,$$delimiter) = split( / /, $val );
  954. $$prefix =~ s/"//g;
  955. $$delimiter =~ s/"//g;
  956. # Experimental
  957. if ( $public_mbxs ) {
  958. # Figure out the public mailbox settings
  959. /\(\((.+)\)\)\s+\(\((.+)\s+\(\((.+)\)\)/;
  960. $public = $3;
  961. $public =~ /"(.+)"\s+"(.+)"/;
  962. $src_public_prefix = $1 if $conn eq $src;
  963. $src_public_delim = $2 if $conn eq $src;
  964. $dst_public_prefix = $1 if $conn eq $dst;
  965. $dst_public_delim = $2 if $conn eq $dst;
  966. }
  967. last;
  968. }
  969. last if /^1 NO|^1 BAD|^\* BYE/;
  970. }
  971. unless ( $$delimiter ) {
  972. # NAMESPACE command is not supported by the server
  973. # so we will have to figure it out another way.
  974. $delim = getDelimiter( $conn );
  975. $$delimiter = $delim;
  976. $$prefix = '';
  977. }
  978. if ( $debug ) {
  979. Log("prefix >$$prefix<");
  980. Log("delim >$$delimiter<");
  981. }
  982. }
  983. sub mailboxName {
  984. my $srcmbx = shift;
  985. my $srcPrefix = shift;
  986. my $srcDelim = shift;
  987. my $dstPrefix = shift;
  988. my $dstDelim = shift;
  989. my $dstmbx;
  990. my $substChar = '_';
  991. if ( $public_mbxs ) {
  992. my ($public_src,$public_dst) = split(/:/, $public_mbxs );
  993. # If the mailbox starts with the public mailbox prefix then
  994. # map it to the public mailbox destination prefix
  995. if ( $srcmbx =~ /^$public_src/ ) {
  996. Log("src: $srcmbx is a public mailbox") if $debug;
  997. $dstmbx = $srcmbx;
  998. $dstmbx =~ s/$public_src/$public_dst/;
  999. Log("dst: $dstmbx") if $debug;
  1000. return $dstmbx;
  1001. }
  1002. }
  1003. # Change the mailbox name if the user has supplied mapping rules.
  1004. if ( $mbx_map{"$srcmbx"} ) {
  1005. $srcmbx = $mbx_map{"$srcmbx"}
  1006. }
  1007. # Adjust the mailbox name if the source and destination server
  1008. # have different mailbox prefixes or hierarchy delimiters.
  1009. if ( ($srcmbx =~ /[$dstDelim]/) and ($dstDelim ne $srcDelim) ) {
  1010. # The mailbox name has a character that is used on the destination
  1011. # as a mailbox hierarchy delimiter. We have to replace it.
  1012. $srcmbx =~ s^[$dstDelim]^$substChar^g;
  1013. }
  1014. if ( $debug ) {
  1015. Log("src mbx $srcmbx");
  1016. Log("src prefix $srcPrefix");
  1017. Log("src delim $srcDelim");
  1018. Log("dst prefix $dstPrefix");
  1019. Log("dst delim $dstDelim");
  1020. }
  1021. $srcmbx =~ s/^$srcPrefix//;
  1022. $srcmbx =~ s/\\$srcDelim/\//g;
  1023. if ( ($srcPrefix eq $dstPrefix) and ($srcDelim eq $dstDelim) ) {
  1024. # No adjustments necessary
  1025. # $dstmbx = $srcmbx;
  1026. if ( lc( $srcmbx ) eq 'inbox' ) {
  1027. $dstmbx = $srcmbx;
  1028. } else {
  1029. $dstmbx = $srcPrefix . $srcmbx;
  1030. }
  1031. if ( $root_mbx ) {
  1032. # Put folders under a 'root' folder on the dst
  1033. $dstmbx =~ s/^$dstPrefix//;
  1034. $dstDelim =~ s/\./\\./g;
  1035. $dstmbx =~ s/^$dstDelim//;
  1036. $dstmbx = $dstPrefix . $root_mbx . $dstDelim . $dstmbx;
  1037. if ( uc($srcmbx) eq 'INBOX' ) {
  1038. # Special case for the INBOX
  1039. $dstmbx =~ s/INBOX$//i;
  1040. $dstmbx =~ s/$dstDelim$//;
  1041. }
  1042. $dstmbx =~ s/\\//g;
  1043. }
  1044. return $dstmbx;
  1045. }
  1046. $srcmbx =~ s#^$srcPrefix##;
  1047. $dstmbx = $srcmbx;
  1048. if ( $srcDelim ne $dstDelim ) {
  1049. # Need to substitute the dst's hierarchy delimiter for the src's one
  1050. $srcDelim = '\\' . $srcDelim if $srcDelim eq '.';
  1051. $dstDelim = "\\" . $dstDelim if $dstDelim eq '.';
  1052. $dstmbx =~ s#$srcDelim#$dstDelim#g;
  1053. $dstmbx =~ s/\\//g;
  1054. }
  1055. if ( $srcPrefix ne $dstPrefix ) {
  1056. # Replace the source prefix with the dest prefix
  1057. $dstmbx =~ s#^$srcPrefix## if $srcPrefix;
  1058. if ( $dstPrefix ) {
  1059. $dstmbx = "$dstPrefix$dstmbx" unless uc($srcmbx) eq 'INBOX';
  1060. }
  1061. $dstDelim = "\\$dstDelim" if $dstDelim eq '.';
  1062. $dstmbx =~ s#^$dstDelim##;
  1063. }
  1064. if ( $root_mbx ) {
  1065. # Put folders under a 'root' folder on the dst
  1066. $dstDelim =~ s/\./\\./g;
  1067. $dstmbx =~ s/^$dstPrefix//;
  1068. $dstmbx =~ s/^$dstDelim//;
  1069. $dstmbx = $dstPrefix . $root_mbx . $dstDelim . $dstmbx;
  1070. if ( uc($srcmbx) eq 'INBOX' ) {
  1071. # Special case for the INBOX
  1072. $dstmbx =~ s/INBOX$//i;
  1073. $dstmbx =~ s/$dstDelim$//;
  1074. }
  1075. $dstmbx =~ s/\\//g;
  1076. }
  1077. return $dstmbx;
  1078. }
  1079. sub flags {
  1080. my $flags = shift;
  1081. my @newflags;
  1082. my $newflags;
  1083. # Make sure the flags list contains standard
  1084. # IMAP flags and optionally custom tags
  1085. return unless $flags;
  1086. $flags =~ s/\\Recent//i;
  1087. foreach $_ ( split(/\s+/, $flags) ) {
  1088. push( @newflags, $_ ) if substr($_,0,1) eq '\\';
  1089. if ( $opt_T ) {
  1090. # Include user-defined flags
  1091. push( @newflags, $_ ) if substr($_,0,1) eq '$';
  1092. }
  1093. }
  1094. $newflags = join( ' ', @newflags );
  1095. $newflags =~ s/\\Deleted//ig if $opt_r;
  1096. $newflags =~ s/^\s+|\s+$//g;
  1097. return $newflags;
  1098. }
  1099. sub map_mbx_names {
  1100. my $mbx_map = shift;
  1101. my $srcDelim = shift;
  1102. my $dstDelim = shift;
  1103. # The -M <file> argument causes imapcopy to read the
  1104. # contents of a file with mappings between source and
  1105. # destination mailbox names. This permits the user to
  1106. # to change the name of a mailbox when copying messages.
  1107. #
  1108. # The lines in the file should be formatted as:
  1109. # <source mailbox name>: <destination mailbox name>
  1110. # For example:
  1111. # Drafts/2008/Save: Draft_Messages/2008/Save
  1112. # Action Items: Inbox
  1113. #
  1114. # Note that if the names contain non-ASCII characters such
  1115. # as accents or diacritical marks then the Perl module
  1116. # Unicode::IMAPUtf7 module must be installed.
  1117. return unless $mbx_map_fn;
  1118. unless ( open(MAP, "<$mbx_map_fn") ) {
  1119. Log("Error opening mbx map file $mbx_map_fn: $!");
  1120. exit;
  1121. }
  1122. $use_utf7 = 0;
  1123. while( <MAP> ) {
  1124. chomp;
  1125. s/[\r\n]$//; # In case we're on Windows
  1126. s/^\s+//;
  1127. next if /^#/;
  1128. next unless $_;
  1129. ($srcmbx,$dstmbx) = split(/\s*:\s*/, $_);
  1130. # Unless the mailbox name is entirely ASCII we'll have to use
  1131. # the Modified UTF-7 character set.
  1132. $use_utf7 = 1 unless isAscii( $srcmbx );
  1133. $use_utf7 = 1 unless isAscii( $dstmbx );
  1134. $srcmbx =~ s/\//$srcDelim/g;
  1135. $dstmbx =~ s/\//$dstDelim/g;
  1136. $$mbx_map{"$srcmbx"} = $dstmbx;
  1137. }
  1138. close MAP;
  1139. if ( $use_utf7 ) {
  1140. eval 'use Unicode::IMAPUtf7';
  1141. if ( $@ ) {
  1142. Log("At least one mailbox map contains non-ASCII characters. This means you");
  1143. Log("have to install the Perl Unicode::IMAPUtf7 module in order to map mailbox ");
  1144. Log("names between the source and destination servers.");
  1145. print "At least one mailbox map contains non-ASCII characters. This means you\n";
  1146. print "have to install the Perl Unicode::IMAPUtf7 module in order to map mailbox\n";
  1147. print "names between the source and destination servers.\n";
  1148. exit;
  1149. }
  1150. }
  1151. my %temp;
  1152. foreach $srcmbx ( keys %$mbx_map ) {
  1153. $dstmbx = $$mbx_map{"$srcmbx"};
  1154. Log("Mapping src:$srcmbx to dst:$dstmbx");
  1155. if ( $use_utf7 ){
  1156. # Encode the name in Modified UTF-7 charset
  1157. $srcmbx = Unicode::IMAPUtf7::imap_utf7_encode( $srcmbx );
  1158. $dstmbx = Unicode::IMAPUtf7::imap_utf7_encode( $dstmbx );
  1159. }
  1160. $temp{"$srcmbx"} = $dstmbx;
  1161. }
  1162. %$mbx_map = %temp;
  1163. %temp = ();
  1164. }
  1165. sub isAscii {
  1166. my $str = shift;
  1167. my $ascii = 1;
  1168. # Determine whether a string contains non-ASCII characters
  1169. my $test = $str;
  1170. $test=~s/\P{IsASCII}/?/g;
  1171. $ascii = 0 unless $test eq $str;
  1172. return $ascii;
  1173. }
  1174. sub getDelimiter {
  1175. my $conn = shift;
  1176. my $delimiter;
  1177. # Issue a 'LIST "" ""' command to find out what the
  1178. # mailbox hierarchy delimiter is.
  1179. sendCommand ($conn, '1 LIST "" ""');
  1180. @response = '';
  1181. while ( 1 ) {
  1182. readResponse ($conn);
  1183. if ( $response =~ /^1 OK/i ) {
  1184. last;
  1185. }
  1186. elsif ( $response !~ /^\*/ ) {
  1187. Log ("unexpected response: $response");
  1188. return 0;
  1189. }
  1190. }
  1191. for $i (0 .. $#response) {
  1192. $response[$i] =~ s/\s+/ /;
  1193. if ( $response[$i] =~ /\* LIST \((.*)\) "(.*)" "(.*)"/i ) {
  1194. $delimiter = $2;
  1195. }
  1196. }
  1197. return $delimiter;
  1198. }
  1199. # Reconnect to the servers after a timeout error.
  1200. #
  1201. sub reconnect {
  1202. my $checkpoint = shift;
  1203. my $conn = shift;
  1204. Log("Attempting to reconnect");
  1205. my ($mbx,$shost,$suser,$spwd,$dhost,$duser,$dpwd) = split(/\|/, $checkpoint);
  1206. close $src;
  1207. close $dst;
  1208. connectToHost($shost,\$src);
  1209. login($suser,$spwd,$shost,$src);
  1210. connectToHost($dhost,\$dst);
  1211. login($duser,$dpwd,$dhost,$dst);
  1212. selectMbx( $mbx, $src );
  1213. createMbx( $mbx, $dst ); # Just in case
  1214. }
  1215. # Handle signals
  1216. sub signalHandler {
  1217. my $sig = shift;
  1218. if ( $sig eq 'ALRM' ) {
  1219. Log("Caught a SIG$sig signal, timeout error");
  1220. $conn_timed_out = 1;
  1221. } else {
  1222. Log("Caught a SIG$sig signal, shutting down");
  1223. exit;
  1224. }
  1225. Log("Resuming");
  1226. }
  1227. sub fixup_date {
  1228. my $date = shift;
  1229. # Make sure the hrs part of the date is 2 digits. At least
  1230. # one IMAP server expects this.
  1231. $$date =~ s/^\s+//;
  1232. $$date =~ /(.+) (.+):(.+):(.+) (.+)/;
  1233. my $hrs = $2;
  1234. return if length( $hrs ) == 2;
  1235. my $newhrs = '0' . $hrs if length( $hrs ) == 1;
  1236. $$date =~ s/ $hrs/ $newhrs/;
  1237. }
  1238. sub init_mbx {
  1239. my $mbx = shift;
  1240. my $conn = shift;
  1241. my @msgs;
  1242. # Remove all messages from a mailbox
  1243. Log("Initializing mailbox $mbx");
  1244. getMsgList( $mbx, \@msgs, $conn, 'SELECT' );
  1245. my $msgcount = $#msgs + 1;
  1246. Log("$mbx has $msgcount messages");
  1247. return if $msgcount == 0; # No messages to delete
  1248. foreach my $msgnum ( @msgs ) {
  1249. ($msgnum) = split(/\|/, $msgnum);
  1250. delete_msg( $msgnum, $conn );
  1251. }
  1252. expungeMbx( $mbx, $conn );
  1253. }
  1254. sub delete_msg_list {
  1255. my $msgnums = shift;
  1256. my $mbx = shift;
  1257. my $conn = shift;
  1258. my $rc;
  1259. # Mark a set of messages for deletion
  1260. selectMbx( $mbx, $conn );
  1261. foreach my $msgnum ( split(/\s+/, $msgnums ) ) {
  1262. sendCommand ( $conn, "1 STORE $msgnum +FLAGS (\\Deleted)");
  1263. while (1) {
  1264. readResponse ($conn);
  1265. if ( $response =~ /^1 OK/i ) {
  1266. $rc = 1;
  1267. Log(" Marked msg number $msgnum for delete") if $debug;
  1268. last;
  1269. }
  1270. if ( $response =~ /^1 BAD|^1 NO/i ) {
  1271. Log("Error setting \Deleted flag for msg $msgnum: $response");
  1272. $rc = 0;
  1273. last;
  1274. }
  1275. }
  1276. }
  1277. return $rc;
  1278. }
  1279. sub expungeMbx {
  1280. my $mbx = shift;
  1281. my $conn = shift;
  1282. Log("Expunging mailbox $mbx");
  1283. sendCommand ($conn, "1 SELECT \"$mbx\"");
  1284. while (1) {
  1285. readResponse ($conn);
  1286. last if ( $response =~ /1 OK/i );
  1287. }
  1288. sendCommand ( $conn, "1 EXPUNGE");
  1289. $expunged=0;
  1290. while (1) {
  1291. readResponse ($conn);
  1292. $expunged++ if $response =~ /\* (.+) Expunge/i;
  1293. last if $response =~ /^1 OK/;
  1294. if ( $response =~ /^1 BAD|^1 NO/i ) {
  1295. Log("Error purging messages: $response");
  1296. last;
  1297. }
  1298. }
  1299. $totalExpunged += $expunged;
  1300. Log("$expunged messages expunged");
  1301. }
  1302. sub cram_md5 {
  1303. my $challenge = shift;
  1304. my $user = shift;
  1305. my $password = shift;
  1306. eval 'use Digest::HMAC_MD5 qw(hmac_md5_hex)';
  1307. use MIME::Base64 qw(decode_base64 encode_base64);
  1308. # Adapated from script by Paul Makepeace <http://paulm.com>, 2002-10-12
  1309. # Takes user, key, and base-64 encoded challenge and returns base-64
  1310. # encoded CRAM. See,
  1311. # IMAP/POP AUTHorize Extension for Simple Challenge/Response:
  1312. # RFC 2195 http://www.faqs.org/rfcs/rfc2195.html
  1313. # SMTP Service Extension for Authentication:
  1314. # RFC 2554 http://www.faqs.org/rfcs/rfc2554.html
  1315. # Args: tim tanstaaftanstaaf PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
  1316. # should yield: dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
  1317. my $challenge_data = decode_base64($challenge);
  1318. my $hmac_digest = hmac_md5_hex($challenge_data, $password);
  1319. my $response = encode_base64("$user $hmac_digest");
  1320. chomp $response;
  1321. if ( $debug ) {
  1322. Log("Challenge: $challenge_data");
  1323. Log("HMAC digest: $hmac_digest");
  1324. Log("CRAM Base64: $response");
  1325. }
  1326. return $response;
  1327. }
  1328. sub validate_date {
  1329. my $date = shift;
  1330. my $invalid;
  1331. # Make sure the "after" date is in DD-MMM-YYYY format
  1332. my ($day,$month,$year) = split(/-/, $date);
  1333. $invalid = 1 unless ( $day > 0 and $day < 32 );
  1334. $invalid = 1 unless $month =~ /Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/i;
  1335. $invalid = 1 unless $year > 1900 and $year < 2999;
  1336. if ( $invalid ) {
  1337. Log("The 'Sent after' date $date must be in DD-MMM-YYYY format");
  1338. exit;
  1339. }
  1340. }
  1341. sub commafy {
  1342. my $number = shift;
  1343. $_ = $$number;
  1344. 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
  1345. $$number = $_;
  1346. }
  1347. sub delete_msg {
  1348. my $msgnum = shift;
  1349. my $conn = shift;
  1350. my $rc;
  1351. sendCommand ( $conn, "1 STORE $msgnum +FLAGS (\\Deleted)");
  1352. while (1) {
  1353. readResponse ($conn);
  1354. if ( $response =~ /^1 OK/i ) {
  1355. $rc = 1;
  1356. Log(" Marked msg number $msgnum for delete") if $debug;
  1357. last;
  1358. }
  1359. if ( $response =~ /^1 BAD|^1 NO/i ) {
  1360. Log("Error setting \Deleted flag for msg $msgnum: $response");
  1361. $rc = 0;
  1362. last;
  1363. }
  1364. }
  1365. return $rc;
  1366. }
  1367. sub resume {
  1368. # Disconnect, re-connect, and log back in.
  1369. Log("Fatal error, lost connection to either the source or destination");
  1370. # Log("checkpoint $checkpoint");
  1371. Log("LAST $LAST");
  1372. my ($mbx,$msgnum) = split(/\|/, $LAST);
  1373. Log("mbx $mbx");
  1374. Log("Disconnect from the source and destination servers");
  1375. close $src;
  1376. close $dst;
  1377. Log("Sleeping 15 seconds before reconnecting");
  1378. sleep 15;
  1379. Log("Reconnect to source server and log back in");
  1380. connectToHost($sourceHost, \$src) or exit;
  1381. login($sourceUser,$sourcePwd, $sourceHost, $src, $srcMethod) or exit;
  1382. selectMbx( $mbx, $src );
  1383. Log("Reconnect to destination server and log back in");
  1384. connectToHost( $destHost, \$dst ) or exit;
  1385. login( $destUser,$destPwd, $destHost, $dst, $dstMethod ) or exit;
  1386. Log("Resuming");
  1387. # Just in case we were creating a mailbox when the connection
  1388. # was lost check and recreate it if necessary
  1389. Log("does $mbx exist?");
  1390. createMbx( $mbx, $dst ) unless mbxExists( $mbx, $dst );
  1391. return;
  1392. }
  1393. # getMsgIdList
  1394. #
  1395. # Get a list of the user's messages in a mailbox
  1396. #
  1397. sub getMsgIdList {
  1398. my $mailbox = shift;
  1399. my $msgids = shift;
  1400. my $conn = shift;
  1401. my $empty;
  1402. my $msgnum;
  1403. my $from;
  1404. my $msgid;
  1405. %$msgids = ();
  1406. sendCommand ($conn, "1 EXAMINE \"$mailbox\"");
  1407. undef @response;
  1408. $empty=0;
  1409. while ( 1 ) {
  1410. readResponse ( $conn );
  1411. if ( $response =~ / 0 EXISTS/i ) { $empty=1; }
  1412. if ( $response =~ /^1 OK/i ) {
  1413. # print STDERR "response $response\n";
  1414. last;
  1415. }
  1416. elsif ( $response !~ /^\*/ ) {
  1417. Log ("unexpected response: $response");
  1418. # print STDERR "Error: $response\n";
  1419. return 0;
  1420. }
  1421. }
  1422. if ( $empty ) {
  1423. return;
  1424. }
  1425. Log("Fetch the header info") if $debug;
  1426. sendCommand ( $conn, "1 FETCH 1:* (body[header.fields (Message-Id)])");
  1427. undef @response;
  1428. while ( 1 ) {
  1429. readResponse ( $conn );
  1430. return if $conn_timed_out;
  1431. if ( $response =~ /^1 OK/i ) {
  1432. last;
  1433. } elsif ( $response =~ /could not be processed/i ) {
  1434. Log("Error: response from server: $response");
  1435. return;
  1436. } elsif ( $response =~ /^1 NO|^1 BAD/i ) {
  1437. return;
  1438. }
  1439. }
  1440. $flags = '';
  1441. for $i (0 .. $#response) {
  1442. $_ = $response[$i];
  1443. last if /OK FETCH complete/;
  1444. if ($response[$i] =~ /Message-ID: (.+)/i) {
  1445. $$msgids{"$1"} = 1;
  1446. }
  1447. }
  1448. }
  1449. sub encode_ampersand {
  1450. my $mbx = shift;
  1451. # The IMAP RFC requires mailbox names with '&' be
  1452. # encoded as '&-'
  1453. # The problem with this routine is a mailbox name may be
  1454. # encoded in Mod UTF7 which uses the '&' character for its
  1455. # own purposes, eg r&AOk-pertoire_XXX. We have to leave it
  1456. # alone. Anyway, this code was inserted because of an IMAP
  1457. # server which did not do its job so the usefulness of this
  1458. # conversion is limited.
  1459. if ( $$mbx =~ /\&/ ) {
  1460. if ( $$mbx !~ /\&-/ ) {
  1461. # Need to encode the '&' as '&-'
  1462. $$mbx =~ s/\&/\&-/g;
  1463. Log("Encoded $$mbx");
  1464. }
  1465. }
  1466. }