Guest User

Untitled

a guest
Jun 7th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.93 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use POSIX;
  5. use Term::ReadKey;
  6. use Mail::IMAPClient;
  7. use IO::Socket::SSL;
  8. use HTTP::Date;
  9. use DateTime;
  10.  
  11. # // SETTINGS
  12. my $server = 'imap.gmail.com';
  13. my $user = 'user@domain.ext';
  14. ####
  15.  
  16.  
  17. my %options = ();
  18.  
  19. check_version();
  20. check_parameters();
  21. check_maildir();
  22.  
  23. my $imap = imap_connect();
  24. if ($options{"label"}) { create_label_if_not_exist($options{"label"}); }
  25.  
  26. my $copy_count = copy_messages();
  27. print "Copied $copy_count messages\n";
  28.  
  29. exit 0;
  30.  
  31. sub check_parameters {
  32.     if (@ARGV < 1 || @ARGV > 2) {
  33.         print "Usage: $0 <maildir> [label]\n";
  34.         exit 1;
  35.     }
  36.  
  37.     if (@ARGV == 2) {
  38.         $options{"label"} = $ARGV[1];
  39.     }
  40. }
  41.  
  42. sub check_maildir {
  43.     chdir($ARGV[0]) or die("$ARGV[0]: $!");
  44.     if (! -d "new" || ! -d "cur") { die("$ARGV[0]: Invalid maildir"); }
  45.     chdir("cur");
  46. }
  47.  
  48. sub imap_connect {
  49.     my $socket = IO::Socket::SSL->new(
  50.         PeerAddr => $server,
  51.         PeerPort => 993,
  52.         SSL_ca_path => '/etc/ssl/certs',
  53.         SSL_verify_mode => 0x01,
  54.     ) or die "Unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
  55.  
  56.     # Read password
  57.     print "Password: ";
  58.     ReadMode('noecho');
  59.     my $pass = <STDIN>;
  60.     chomp($pass);
  61.     ReadMode('normal');
  62.     print "\n";
  63.  
  64.     my $imap = Mail::IMAPClient->new(Socket => $socket) or die "new(): $@\n";
  65.     $imap->User($user);
  66.     $imap->Password($pass);
  67.     $imap->State(Mail::IMAPClient::Connected());
  68.     $imap->login() or die 'login(): ' . $imap->LastError(). "\n";
  69.  
  70.     return $imap;
  71. }
  72.  
  73. sub create_label_if_not_exist {
  74.     my ($label) = @_;
  75.     my $exist = 0;
  76.  
  77.     foreach ($imap->folders) {
  78.         if ($_ eq $label) {
  79.             $exist = 1;
  80.             last;
  81.         }
  82.     }
  83.  
  84.     if (! $exist) {
  85.         print "Creating label $label\n";
  86.         $imap->create($label) or die("Unable to create label: $@");
  87.     } else {
  88.         print "Label $label exists\n";
  89.     }
  90. }
  91.  
  92. sub copy_messages {
  93.     my $count = 0;
  94.     my $label = ($options{"label"} ? "INBOX.".$options{"label"} : "INBOX");
  95.  
  96.     # copy each message found in the Maildir/cur directory
  97.     foreach (<*>) {
  98.         # message has already been copied
  99.         if (-f ".$_.gmailmig") {
  100.             print "Skipping $_\n";
  101.             next;
  102.         }
  103.  
  104.         # find flags for this message
  105.         my @flags;
  106.         $_ =~ m/,([A-Z]+)$/;
  107.         my $matches = $1;
  108.         if ($matches =~ /F/) { push(@flags, '\Flagged'); }
  109.         if ($matches =~ /R/) { push(@flags, '\Answered'); }
  110.         if ($matches =~ /S/) { push(@flags, '\Seen'); }
  111.  
  112.         # copy message
  113.         print "Copying message $_ with label $label and flags ". join(" ", @flags)."\n";
  114.  
  115.         # select label
  116.         $imap->select($label);
  117.  
  118.         my $msg_uid = $imap->append_file($label, $_, undef, join(" ", @flags), 1);
  119.         if ( ! $msg_uid) {
  120.             print STDERR "$_: Could not copy: " . $imap->LastError(). "\n";
  121.             last;
  122.         }
  123.  
  124.         # success
  125.         open FILE, ">.$_.gmailmig" or die $!;
  126.         print FILE $msg_uid;
  127.         close FILE;
  128.         $count++;
  129.     }
  130.  
  131.     return $count;
  132. }
  133.  
  134. sub check_version {
  135.     my $version = $Mail::IMAPClient::VERSION;
  136.     if (! ($version =~ /^3\./)) { die("Mail::IMAPClient $version installed but >= 3.x required") }
  137. }
Add Comment
Please, Sign In to add comment