Guest User

Untitled

a guest
Jun 7th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 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. # copy message
  105. print "Copying message $_ with label $label\n";
  106.  
  107. # select label
  108. $imap->select($label);
  109.  
  110. my $msg_uid = $imap->append_file($label, $_, undef, '\Seen', 1);
  111. if ( ! $msg_uid) {
  112. print STDERR "$_: Could not copy: " . $imap->LastError(). "\n";
  113. return $count;
  114. }
  115.  
  116. # success
  117. open FILE, ">.$_.gmailmig" or die $!;
  118. print FILE $msg_uid;
  119. close FILE;
  120. $count++;
  121. }
  122.  
  123. return $count;
  124. }
  125.  
  126. sub check_version {
  127. my $version = $Mail::IMAPClient::VERSION;
  128. if (! ($version =~ /^3\./)) { die("Mail::IMAPClient $version installed but >= 3.x required") }
  129. }
Add Comment
Please, Sign In to add comment