perl/keyring/kdewallet / sample /

# Simple keyring usage example: check IMAP folder for newest emails.
# Non-sensitive data are taken from program args, but password is 
# got directly from the user and saved in keyring.
# Try running app a few times. Only on first run there should be
# password prompt.

    package RunMe;
    use Moose;
    with 'MooseX::Getopt';

    use Passwd::Keyring::KDEWallet; # Keyring itself
    use Term::ReadKey;              # For secure password prompt
    use Net::IMAP::Simple::SSL;     # We access IMAP as illustration
    use Email::Simple;              # and get some email data.

    has 'machine' => (
        is=>'ro', isa=>'Str', required=>1,
        documentation=>'IMAP machine name, like');
    has 'port' =>  (is=>'ro', isa=>'Int', required=>1, default=> 993,
                   documentation=>'IMAP port (default 993)');
    has 'user' => (is=>'ro', isa=>'Str', required=>1,
                   documentation=>'Your username on IMAP server');
    has 'folder' => (is=>'ro', isa=>'Str', required=>1, default=>'INBOX',
                     documentation=>'The folder to check');
    has 'count' => (is=>'ro', isa=>'Int', required=>1, default=> 5,
                    documentation=>'Number of emails printed');

    my $GROUP = 'Passwd::Keyring::KDEWallet tests and samples';
    my $APP = 'imap_query_kdewallet_sample';

    my $ATTEMPTS_COUNT = 3;

    sub run {
        my $self = shift;

        my $keyring = Passwd::Keyring::KDEWallet->new(
            app=>$APP, group=>$GROUP);

        my $imap_addr = $self->machine . ':' . $self->port;
        my $imap = Net::IMAP::Simple::SSL->new($imap_addr)
          or die "Can't connect to $imap_addr: $Net::IMAP::Simple::errstr\n";

        my $realm = "IMAP:$imap_addr";
        my $user = $self->user;
        # Attempt to recover previously saved password
        my $password = $keyring->get_password($user, $realm);
        for (my $attempt_no = 1; 1; ++ $attempt_no) {
            unless($password) {
                print "Enter password for $user on $realm: ";
                ReadMode 'noecho';
                $password = ReadLine 0; chomp($password);
                ReadMode 'normal';
                print "\n";
            if( $imap->login($user, $password) ) {
                # Saving the password for future
                $keyring->set_password($user, $password, $realm);
                last; # Password OK, continuing work
            } else {
                print "IMAP login failed (bad password?). Error: " . $imap->errstr, "\n";
                # Clearing the bad password in case it was taken from keyring
                $keyring->clear_password($self->user, $realm);
                $password = '';
                # Retrying unless we exhaused attempts
                if($attempt_no >= $ATTEMPTS_COUNT) {
                    die "$attempt_no login failures, good bye\n";

        # Actual work, as illustration

        my $folder = $self->folder;
        my $count = $imap->select($folder);
        unless(defined($count)) {
            die "Unable to open folder $folder (maybe bad name?): $Net::IMAP::Simple::errstr\n";
        print "$count messages in $folder. Newest:\n";
        for(my $idx = $count;
            $idx > 0 && $idx > $count - $self->count;
            -- $idx) {
            my $email = Email::Simple->new(join '', @{ $imap->top($idx) } );
            printf("[%03d] %s\n", $idx, $email->header('Subject'));

my $run_me = RunMe->new_with_options();
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.