Subversion Repositories sysadmin_scripts

Rev

Rev 42 | Rev 51 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 42 Rev 50
Line 10... Line 10...
10
#
10
#
11
#    version 1.0.1 20140819
11
#    version 1.0.1 20140819
12
#        Removed dependancy on Email::Simple
12
#        Removed dependancy on Email::Simple
13
#        Allowed 'separator' as an element in either source or target
13
#        Allowed 'separator' as an element in either source or target
14
#
14
#
-
 
15
#    version 2.0.0 20190817 RWR
-
 
16
#        Major revision.
-
 
17
#           Config is now YAML
-
 
18
#           Default section which will fill in the blanks for anything not filled in on an account, so creating a lot of accounts
-
 
19
#              with common values is easier to set up an maintain
-
 
20
#           Target folder is configurable on a per account basis, using tags <folder>, <year>, <month> (called hierachy)
-
 
21
#
15
#    This program is free software: you can redistribute it and/or modify
22
#    This program is free software: you can redistribute it and/or modify
16
#    it under the terms of the GNU General Public License as published by
23
#    it under the terms of the GNU General Public License as published by
17
#    the Free Software Foundation, either version 3 of the License, or
24
#    the Free Software Foundation, either version 3 of the License, or
18
#    (at your option) any later version.
25
#    (at your option) any later version.
19
#
26
#
Line 194... Line 201...
194
   my $msgDate = $email->header('Date');
201
   my $msgDate = $email->header('Date');
195
   my @t = strptime( $msgDate );
202
   my @t = strptime( $msgDate );
196
   my $month = $t[4]+1;
203
   my $month = $t[4]+1;
197
   $month = '0' . $month if $month < 10;
204
   $month = '0' . $month if $month < 10;
198
   my $year = $t[5]+1900;
205
   my $year = $t[5]+1900;
199
   # now, build the path on the new machine
206
   # also, may need the source hierarchy
200
   my $sourceFolder = join ( $target->{'separator'}, @{ $source->{'source folder list'} } );
207
   my $sourceFolder = join ( $target->{'separator'}, @{ $source->{'source folder list'} } );
-
 
208
   # now, build the path on the new machine
201
   my $targetPattern = join( $target->{'separator'}, @{ $target->{'hierachy pattern'} } );
209
   my $targetPattern = join( $target->{'separator'}, @{ $target->{'hierachy pattern'} } );
202
 
-
 
203
   $targetPattern =~ s/<path>/$sourceFolder/gi;
210
   $targetPattern =~ s/<path>/$sourceFolder/gi;
204
   $targetPattern =~ s/<month>/$month/gi;
211
   $targetPattern =~ s/<month>/$month/gi;
205
   $targetPattern =~ s/<year>/$year/gi;
212
   $targetPattern =~ s/<year>/$year/gi;
206
   # return the string we created, separated by
213
   # return the string we created, separated by
207
   # the delimiters for the target
214
   # the delimiters for the target
Line 248... Line 255...
248
      # get the message
255
      # get the message
249
      my $message = $sourceAccount->get( $id ) or die $sourceAccount->errstr;
256
      my $message = $sourceAccount->get( $id ) or die $sourceAccount->errstr;
250
      # calculate where we are going to move this to
257
      # calculate where we are going to move this to
251
      my $targetFolder = &calculateTargetFolder( $message, $source, $target );
258
      my $targetFolder = &calculateTargetFolder( $message, $source, $target );
252
      if ( $TESTING ) {
259
      if ( $TESTING ) {
253
         print "Would have moved message to $targetFolder\n";
260
         print "Would have " . ( $source->{'deleteOnSuccess'} ? 'moved' : 'copied' )  . " message to $targetFolder\n";
254
         next;
261
         next;
255
      }
262
      }
256
      if ( $target->{'connection'}->select( $folder ) || &makeFolder( $target->{'connection'}, $folder, $target->{'separator'} ) ) {
263
      if ( $target->{'connection'}->select( $targetFolder ) || &makeFolder( $target->{'connection'}, $targetFolder, $target->{'separator'} ) ) {
257
         if ( $target->{'connection'}->put( $folder, $message, @flags ) ) {
264
         if ( $target->{'connection'}->put( $targetFolder, $message, @flags ) ) {
258
            $source->{'connection'}->delete( $id ) if ( $source->{'deleteOnSuccess'} ) ;
265
            $source->{'connection'}->delete( $id ) if ( $source->{'deleteOnSuccess'} ) ;
259
            $numMessages++;
266
            $numMessages++;
260
         } else {
267
         } else {
261
            die "Could not write to target, aborting\n$target->{'connection'}->errstr\n";
268
            die "Could not write to target, aborting\n$targetFolder->{'connection'}->errstr\n";
262
         }
269
         }
263
      } else {
270
      } else {
264
         warn "\t\t$folder not found in target and could not create it\n";
271
         warn "\t\t$targetFolder not found in target and could not create it\n";
265
      }
272
      }
266
 
273
 
267
   }
274
   }
268
   return $numMessages;
275
   return $numMessages;
269
}
276
}
Line 340... Line 347...
340
sub processAccount {
347
sub processAccount {
341
   my $account = shift;
348
   my $account = shift;
342
 
349
 
343
   next unless $account->{'enabled'}; # blow it off if it is not enabled
350
   next unless $account->{'enabled'}; # blow it off if it is not enabled
344
   my $TESTING = $account->{'testing'}; # create mini global if we should test this account
351
   my $TESTING = $account->{'testing'}; # create mini global if we should test this account
-
 
352
 
-
 
353
   print "========= Test Mode ========\n" if $TESTING;
345
   
354
   
346
   # open and log into both source and target, and get the separator used
355
   # open and log into both source and target, and get the separator used
347
   foreach my $acct ( 'target','source' ) {
356
   foreach my $acct ( 'target','source' ) {
348
      $account->{$acct}->{'connection'} = &openIMAPConnection( $account->{$acct}->{'server'}, $account->{$acct}->{'username'}, $account->{$acct}->{'password'} );
357
      $account->{$acct}->{'connection'} = &openIMAPConnection( $account->{$acct}->{'server'}, $account->{$acct}->{'username'}, $account->{$acct}->{'password'} );
349
      unless ( $account->{$acct}->{'connection'} ) {
358
      unless ( $account->{$acct}->{'connection'} ) {
Line 358... Line 367...
358
   my $target = $account->{'target'};
367
   my $target = $account->{'target'};
359
 
368
 
360
   my %temp = map{ lc($_) => 1 } @{$source->{'system'}};
369
   my %temp = map{ lc($_) => 1 } @{$source->{'system'}};
361
   $source->{'system folders'} = \%temp;
370
   $source->{'system folders'} = \%temp;
362
 
371
 
363
   $source->{'before date'} = &getDate( $account->{'age'} );
372
   $source->{'before date'} = &getDate( $source->{'age'} );
364
   print "\t" . ( $source->{'deleteOnSuccess'} ? 'Moving' : 'Copying' ) . " all messages before $source->{'before date'}\n";
373
   print "\t" . ( $source->{'deleteOnSuccess'} ? 'Moving' : 'Copying' ) . " all messages before $source->{'before date'}\n";
365
 
374
 
366
   # get a list of all folders to be processed on the source
375
   # get a list of all folders to be processed on the source
367
   $source->{'folders'} = &getFolders( $source->{'connection'}, $source->{'ignore'}, $source->{'separator'} );
376
   $source->{'folders'} = &getFolders( $source->{'connection'}, $source->{'ignore'}, $source->{'separator'} );
368
 
377
 
-
 
378
   if ( $TESTING ) {
-
 
379
      print Dumper( $source );
-
 
380
      print "Source above, press enter to continue: "; my $j = <STDIN>;
-
 
381
      print Dumper( $target );
-
 
382
      print "Target above, Press enter to continue: "; $j = <STDIN>;
-
 
383
   }
369
 
384
 
370
   my $folderList = $source->{'folders'};
385
   my $folderList = $source->{'folders'};
371
   my $count = 0; # count the number of messages processed
386
   my $count = 0; # count the number of messages processed
372
   my $processedCount = 0; # count the number of folders processed
387
   my $processedCount = 0; # count the number of folders processed
373
   foreach my $folder ( @$folderList ) {
388
   foreach my $folder ( @$folderList ) {
374
      my $messages;
389
      my $messages;
375
      $messages = &processFolder( $source, $target, $folder, $TESTING ); #, $date, $$source{'separator'}, $$target{'separator'}, $deleteOnSuccess );
390
      $messages = &processFolder( $source, $target, $folder, $TESTING ); #, $date, $$source{'separator'}, $$target{'separator'}, $deleteOnSuccess );
376
 
391
 
377
      $TESTING ? print "Would expunge $folder\n" : $source->{'connection'}->expunge_mailbox( $folder );
392
      $TESTING ? print "Would expunge $folder\n" : $source->{'connection'}->expunge_mailbox( $folder );
378
      # delete folder if empty and client has requested it.
393
      # delete folder if empty and client has requested it.
379
      ( $TESTING ? print "Would delete $folder\n" : &deleteAFolder( $source, $folder, $TESTING ) ) if $account->{'source'}->{'deleteEmptyFolders'};
394
      &deleteAFolder( $source, $folder, $TESTING ) if $account->{'source'}->{'deleteEmptyFolders'};
380
      print "\n\t\t$messages processed\n";
395
      print "\n\t\t$messages processed\n";
381
      $count += $messages;
396
      $count += $messages;
382
      $processedCount++;
397
      $processedCount++;
383
      # next line used only for testing. Dies after 5 folders on first account
398
      # next line used only for testing. Dies after 5 folders on first account
384
      last if $processedCount > 5;
399
      last if $processedCount > 5 and $TESTING;
385
   }
400
   }
386
 
401
 
387
   $source->{'connection'}->quit;
402
   $source->{'connection'}->quit;
388
   $target->{'connection'}->quit;
403
   $target->{'connection'}->quit;
389
   return $count;
404
   return $count;
Line 410... Line 425...
410
$processed{'Messages'} = 0;
425
$processed{'Messages'} = 0;
411
 
426
 
412
# grab only the accounts for simplicity
427
# grab only the accounts for simplicity
413
my $accounts = $config->{'accounts'};
428
my $accounts = $config->{'accounts'};
414
 
429
 
415
print Dumper( $config ) ; die;
430
#die Dumper( $accounts );
416
 
431
 
417
# now, process each in turn
432
# now, process each in turn
418
foreach my $account ( keys %$accounts ) {
433
foreach my $account ( keys %$accounts ) {
419
   # talk to user
434
   # talk to user
420
   print "Processing account $account\n";
435
   print "Processing account $account\n";