| 1 | #!perl | ||
| 2 | # | ||
| 3 | # Simple keyring usage example: check IMAP folder for newest emails. | ||
| 4 | # Non-sensitive data are taken from program args, but password is | ||
| 5 | # got directly from the user and saved in keyring. | ||
| 6 | # | ||
| 7 | # Try running app a few times. Only on first run there should be | ||
| 8 | # password prompt. | ||
| 9 | # | ||
| 10 | # Example usage (for gmail): | ||
| 11 | # | ||
| 12 | # perl imap_query_kdewallet_sample.pl --machine imap.gmail.com --user joe2774 | ||
| 13 | |||
| 14 | { | ||
| 15 | package RunMe; | ||
| 16 | use Moose; | ||
| 17 | with 'MooseX::Getopt'; | ||
| 18 | |||
| 19 | use Passwd::Keyring::KDEWallet; # Keyring itself | ||
| 20 | use Term::ReadKey; # For secure password prompt | ||
| 21 | use Net::IMAP::Simple::SSL; # We access IMAP as illustration | ||
| 22 | use Email::Simple; # and get some email data. | ||
| 23 | |||
| 24 | has 'machine' => ( | ||
| 25 | is=>'ro', isa=>'Str', required=>1, | ||
| 26 | documentation=>'IMAP machine name, like imap.gmail.com'); | ||
| 27 | has 'port' => (is=>'ro', isa=>'Int', required=>1, default=> 993, | ||
| 28 | documentation=>'IMAP port (default 993)'); | ||
| 29 | has 'user' => (is=>'ro', isa=>'Str', required=>1, | ||
| 30 | documentation=>'Your username on IMAP server'); | ||
| 31 | has 'folder' => (is=>'ro', isa=>'Str', required=>1, default=>'INBOX', | ||
| 32 | documentation=>'The folder to check'); | ||
| 33 | has 'count' => (is=>'ro', isa=>'Int', required=>1, default=> 5, | ||
| 34 | documentation=>'Number of emails printed'); | ||
| 35 | |||
| 36 | has 'kwalletd' => (is=>'ro', isa=>'Str', | ||
| 37 | documentation=>'Path to kwalletd binary (used if script must start it)'); | ||
| 38 | |||
| 39 | my $GROUP = 'Passwd::Keyring tests and samples'; | ||
| 40 | my $APP = 'imap_query_kdewallet_sample'; | ||
| 41 | |||
| 42 | my $ATTEMPTS_COUNT = 3; | ||
| 43 | |||
| 44 | sub run { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | my $keyring = Passwd::Keyring::KDEWallet->new( | ||
| 48 | app=>$APP, | ||
| 49 | group=>$GROUP, | ||
| 50 | ($self->kwalletd ? (kwalletd_path => $self->kwalletd): ()) | ||
| 51 | ); | ||
| 52 | |||
| 53 | my $imap_addr = $self->machine . ':' . $self->port; | ||
| 54 | my $imap = Net::IMAP::Simple::SSL->new($imap_addr) | ||
| 55 | or die "Can't connect to $imap_addr: $Net::IMAP::Simple::errstr\n"; | ||
| 56 | |||
| 57 | my $realm = "IMAP:$imap_addr"; | ||
| 58 | my $user = $self->user; | ||
| 59 | # Attempt to recover previously saved password | ||
| 60 | my $password = $keyring->get_password($user, $realm); | ||
| 61 | for (my $attempt_no = 1; 1; ++ $attempt_no) { | ||
| 62 | unless($password) { | ||
| 63 | print "Enter password for $user on $realm: "; | ||
| 64 | ReadMode 'noecho'; | ||
| 65 | $password = ReadLine 0; chomp($password); | ||
| 66 | ReadMode 'normal'; | ||
| 67 | print "\n"; | ||
| 68 | } | ||
| 69 | if( $imap->login($user, $password) ) { | ||
| 70 | # Saving the password for future | ||
| 71 | $keyring->set_password($user, $password, $realm); | ||
| 72 | last; # Password OK, continuing work | ||
| 73 | } else { | ||
| 74 | print "IMAP login failed (bad password?). Error: " . $imap->errstr, "\n"; | ||
| 75 | # Clearing the bad password in case it was taken from keyring | ||
| 76 | $keyring->clear_password($self->user, $realm); | ||
| 77 | $password = ''; | ||
| 78 | # Retrying unless we exhaused attempts | ||
| 79 | if($attempt_no >= $ATTEMPTS_COUNT) { | ||
| 80 | die "$attempt_no login failures, good bye\n"; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | # Actual work, as illustration | ||
| 86 | |||
| 87 | my $folder = $self->folder; | ||
| 88 | my $count = $imap->select($folder); | ||
| 89 | unless(defined($count)) { | ||
| 90 | die "Unable to open folder $folder (maybe bad name?): $Net::IMAP::Simple::errstr\n"; | ||
| 91 | } | ||
| 92 | print "$count messages in $folder. Newest:\n"; | ||
| 93 | for(my $idx = $count; | ||
| 94 | $idx > 0 && $idx > $count - $self->count; | ||
| 95 | -- $idx) { | ||
| 96 | my $email = Email::Simple->new(join '', @{ $imap->top($idx) } ); | ||
| 97 | printf("[%03d] %s\n", $idx, $email->header('Subject')); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | my $run_me = RunMe->new_with_options(); | ||
| 103 | $run_me->run; |