Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use POSIX;
- use Term::ReadKey;
- use Mail::IMAPClient;
- use IO::Socket::SSL;
- use HTTP::Date;
- use DateTime;
- # // SETTINGS
- my $server = 'imap.gmail.com';
- my $user = 'user@domain.ext';
- ####
- my %options = ();
- check_version();
- check_parameters();
- check_maildir();
- my $imap = imap_connect();
- if ($options{"label"}) { create_label_if_not_exist($options{"label"}); }
- my $copy_count = copy_messages();
- print "Copied $copy_count messages\n";
- exit 0;
- sub check_parameters {
- if (@ARGV < 1 || @ARGV > 2) {
- print "Usage: $0 <maildir> [label]\n";
- exit 1;
- }
- if (@ARGV == 2) {
- $options{"label"} = $ARGV[1];
- }
- }
- sub check_maildir {
- chdir($ARGV[0]) or die("$ARGV[0]: $!");
- if (! -d "new" || ! -d "cur") { die("$ARGV[0]: Invalid maildir"); }
- chdir("cur");
- }
- sub imap_connect {
- my $socket = IO::Socket::SSL->new(
- PeerAddr => $server,
- PeerPort => 993,
- SSL_ca_path => '/etc/ssl/certs',
- SSL_verify_mode => 0x01,
- ) or die "Unable to create socket: ", &IO::Socket::SSL::errstr, "\n";
- # Read password
- print "Password: ";
- ReadMode('noecho');
- my $pass = <STDIN>;
- chomp($pass);
- ReadMode('normal');
- print "\n";
- my $imap = Mail::IMAPClient->new(Socket => $socket) or die "new(): $@\n";
- $imap->User($user);
- $imap->Password($pass);
- $imap->State(Mail::IMAPClient::Connected());
- $imap->login() or die 'login(): ' . $imap->LastError(). "\n";
- return $imap;
- }
- sub create_label_if_not_exist {
- my ($label) = @_;
- my $exist = 0;
- foreach ($imap->folders) {
- if ($_ eq $label) {
- $exist = 1;
- last;
- }
- }
- if (! $exist) {
- print "Creating label $label\n";
- $imap->create($label) or die("Unable to create label: $@");
- } else {
- print "Label $label exists\n";
- }
- }
- sub copy_messages {
- my $count = 0;
- my $label = ($options{"label"} ? "INBOX.".$options{"label"} : "INBOX");
- # copy each message found in the Maildir/cur directory
- foreach (<*>) {
- # message has already been copied
- if (-f ".$_.gmailmig") {
- print "Skipping $_\n";
- next;
- }
- # find flags for this message
- my @flags;
- $_ =~ m/,([A-Z]+)$/;
- my $matches = $1;
- if ($matches =~ /F/) { push(@flags, '\Flagged'); }
- if ($matches =~ /R/) { push(@flags, '\Answered'); }
- if ($matches =~ /S/) { push(@flags, '\Seen'); }
- # copy message
- print "Copying message $_ with label $label and flags ". join(" ", @flags)."\n";
- # select label
- $imap->select($label);
- my $msg_uid = $imap->append_file($label, $_, undef, join(" ", @flags), 1);
- if ( ! $msg_uid) {
- print STDERR "$_: Could not copy: " . $imap->LastError(). "\n";
- last;
- }
- # success
- open FILE, ">.$_.gmailmig" or die $!;
- print FILE $msg_uid;
- close FILE;
- $count++;
- }
- return $count;
- }
- sub check_version {
- my $version = $Mail::IMAPClient::VERSION;
- if (! ($version =~ /^3\./)) { die("Mail::IMAPClient $version installed but >= 3.x required") }
- }
Add Comment
Please, Sign In to add comment