auto/share/dist/Passwd-Keyring-PWSafe3/imap_query_example.pl untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 88 CC 5 SCAR 27.7
CC
5
Cov
0%
run
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.0
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 # This sample does not make much sense (user must provide master password
8 # to protect single password), for true benefits one should use many
9 # passwords in single script (then user would provide single master
10 # password instead of many). Still I keep it for comparison with other
11 # backends.
12 #
13 # Some example utilizing many different passwords - welcome
14 #
15 # Example usage (for gmail):
16 #
17 # perl imap_query_pwsafe3_sample.pl --machine imap.gmail.com --user joe2774
18  
19 {
20 package RunMe;
21 use Moose;
22 with 'MooseX::Getopt';
23  
24 use Passwd::Keyring::PWSafe3; # Keyring itself
25 use Term::ReadKey; # For secure password prompt
26 use Net::IMAP::Simple::SSL; # We access IMAP as illustration
27 use Email::Simple; # and get some email data.
28  
29 has 'machine' => (
30 is=>'ro', isa=>'Str', required=>1,
31 documentation=>'IMAP machine name, like imap.gmail.com');
32 has 'port' => (is=>'ro', isa=>'Int', required=>1, default=> 993,
33 documentation=>'IMAP port (default 993)');
34 has 'user' => (is=>'ro', isa=>'Str', required=>1,
35 documentation=>'Your username on IMAP server');
36 has 'folder' => (is=>'ro', isa=>'Str', required=>1, default=>'INBOX',
37 documentation=>'The folder to check');
38 has 'count' => (is=>'ro', isa=>'Int', required=>1, default=> 5,
39 documentation=>'Number of emails printed');
40  
41 my $GROUP = 'Passwd::Keyring tests and samples';
42 my $APP = 'imap_query_pwsafe3_sample';
43  
44 my $ATTEMPTS_COUNT = 3;
45  
46 sub run {
47 my $self = shift;
48  
49 my $keyring = Passwd::Keyring::PWSafe3->new(
50 app=>$APP, group=>$GROUP);
51  
52 my $imap_addr = $self->machine . ':' . $self->port;
53 my $imap = Net::IMAP::Simple::SSL->new($imap_addr)
54 or die "Can't connect to $imap_addr: $Net::IMAP::Simple::errstr\n";
55  
56 my $realm = "IMAP:$imap_addr";
57 my $user = $self->user;
58 # Attempt to recover previously saved password
59 my $password = $keyring->get_password($user, $realm);
60 for (my $attempt_no = 1; 1; ++ $attempt_no) {
61 unless($password) {
62 print "Enter password for $user on $realm: ";
63 ReadMode 'noecho';
64 $password = ReadLine 0; chomp($password);
65 ReadMode 'normal';
66 print "\n";
67 }
68 if( $imap->login($user, $password) ) {
69 # Saving the password for future
70 $keyring->set_password($user, $password, $realm);
71 last; # Password OK, continuing work
72 } else {
73 print "IMAP login failed (bad password?). Error: " . $imap->errstr, "\n";
74 # Clearing the bad password in case it was taken from keyring
75 $keyring->clear_password($self->user, $realm);
76 $password = '';
77 # Retrying unless we exhaused attempts
78 if($attempt_no >= $ATTEMPTS_COUNT) {
79 die "$attempt_no login failures, good bye\n";
80 }
81 }
82 }
83  
84 # Actual work, as illustration
85  
86 my $folder = $self->folder;
87 my $count = $imap->select($folder);
88 unless(defined($count)) {
89 die "Unable to open folder $folder (maybe bad name?): $Net::IMAP::Simple::errstr\n";
90 }
91 print "$count messages in $folder. Newest:\n";
92 for(my $idx = $count;
93 $idx > 0 && $idx > $count - $self->count;
94 -- $idx) {
95 my $email = Email::Simple->new(join '', @{ $imap->top($idx) } );
96 printf("[%03d] %s\n", $idx, $email->header('Subject'));
97 }
98 }
99 }
100  
101 my $run_me = RunMe->new_with_options();
102 $run_me->run;