[BACK]Return to example3.pl CVS log [TXT][DIR] Up to [local] / palm / Palm-Keyring / examples

Annotation of palm/Palm-Keyring/examples/example3.pl, Revision 1.8

1.1       andrew      1: #!/usr/bin/perl
1.8     ! andrew      2: # $RedRiver: example3.pl,v 1.7 2007/12/04 03:37:48 andrew Exp $
1.4       andrew      3: ########################################################################
                      4: # palmkeyring.pl *** a command line client for Keyring databases.
                      5: #
                      6: # 2007.02.10 #*#*# andrew fresh <andrew@cpan.org>
                      7: ########################################################################
                      8: # Copyright (C) 2007 by Andrew Fresh
                      9: #
                     10: # This program is free software; you can redistribute it and/or modify
                     11: # it under the same terms as Perl itself.
                     12: ########################################################################
1.1       andrew     13: use strict;
                     14: use warnings;
1.4       andrew     15:
1.1       andrew     16: use Getopt::Long;
                     17: Getopt::Long::Configure('bundling');
                     18: use Term::ReadLine;
                     19:
                     20: use YAML;
                     21:
1.2       andrew     22: use Palm::PDB;
1.1       andrew     23: use Palm::Keyring;
                     24:
                     25: my $Default_File = $ENV{'HOME'} . '/.jpilot/Keys-Gtkr.pdb';
                     26: my $PDBFile;
                     27: my $Categories;
                     28: my $Names;
1.8     ! andrew     29: my $Accounts;
1.1       andrew     30: my $Action_List;
                     31: my $Action_Show;
                     32:
                     33: my $result = GetOptions (
                     34:        "file|f=s"        => \$PDBFile,
                     35:        "categories|c:s@" => \$Categories,
                     36:        "name|n=s@"       => \$Names,
1.8     ! andrew     37:        "account|a=s@"    => \$Accounts,
1.1       andrew     38:        "list|l"          => \$Action_List,
                     39:        "show|s"          => \$Action_Show,
                     40: );
                     41:
                     42: $PDBFile ||= $Default_File;
1.2       andrew     43: my $pdb = new Palm::PDB();
1.1       andrew     44: $pdb->Load($PDBFile) || die "Couldn't load '$PDBFile': $!";
                     45:
                     46: if ($Action_List) {
                     47:        show_list();
                     48: } elsif ($Action_Show) {
                     49:        show_items($Names);
                     50: } elsif (defined $Categories) {
                     51:        show_categories();
                     52: } else {
                     53:        help();
                     54: }
                     55:
                     56: exit;
                     57:
                     58:
                     59: sub show_list
                     60: {
                     61:        print "Showing List\n";
1.5       andrew     62:        foreach my $r (@{ $pdb->{records} }) {
1.1       andrew     63:                my $category =
1.5       andrew     64:                        $pdb->{appinfo}->{categories}->[ $r->{category} ]->{name};
1.1       andrew     65:
                     66:                my $matched = 0;
                     67:                foreach my $cat (@{ $Categories }) {
                     68:                        $matched++ if uc($category) eq uc($cat);
                     69:                }
                     70:                foreach my $name (@{ $Names}) {
1.3       andrew     71:                        $matched++ if uc($r->{'name'}) eq uc($name);
1.1       andrew     72:                }
                     73:                next if ( @{ $Categories } || @{ $Names } ) && not $matched;
                     74:
                     75:                # XXX Fix up formatting
1.6       andrew     76:                print $r->{plaintext}->{0}->{data} .
1.1       andrew     77:                        ":" .
1.5       andrew     78:                        $r->{category} .
1.1       andrew     79:                        ":" .
                     80:                        $category .
                     81:                        "\n";
                     82:
                     83:        }
                     84: }
                     85:
                     86: sub show_categories
                     87: {
                     88:        foreach my $c (sort @{ $pdb->{'appinfo'}->{'categories'} }) {
                     89:                next unless $c->{'name'};
                     90:                # Fix formatting
                     91:                print $c->{'name'}, "\n";
                     92:        }
                     93: }
                     94:
                     95: sub show_items
                     96: {
1.2       andrew     97:        get_password() || die "Couldn't decrypt file!";
                     98:
                     99:        foreach (0..$#{ $pdb->{'records'} }) {
                    100:         my $r = $pdb->{'records'}->[$_];
1.1       andrew    101:
                    102:                my $category =
                    103:                        $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
                    104:
                    105:                my $matched = 0;
                    106:                foreach my $cat (@{ $Categories }) {
                    107:                        $matched++ if uc($category) eq uc($cat);
                    108:                }
                    109:                foreach my $name (@{ $Names}) {
1.6       andrew    110:                        $matched++ if uc($r->{plaintext}->{0}->{data}) eq uc($name);
1.1       andrew    111:                }
                    112:                next if ( @{ $Categories } || @{ $Names } ) && not $matched;
                    113:
1.2       andrew    114:         my $a = $pdb->Decrypt($r);
1.8     ! andrew    115:         $matched = 0;
        !           116:
        !           117:                foreach my $account (@{ $Accounts }) {
        !           118:                        $matched++ if uc($r->{plaintext}->{1}->{data}) eq uc($account);
        !           119:                }
        !           120:                next if ( @{ $Accounts } ) && not $matched;
1.2       andrew    121:
1.1       andrew    122:                # XXX Fix up formatting
1.5       andrew    123:                print $a->{0}->{data} .  "\n\t" .
1.2       andrew    124:                        "Category: " . $category .  "\n\t" .
1.5       andrew    125:                        "Account:  " . $a->{1}->{data} .  "\n\t" .
                    126:                        "Password: " . $a->{2}->{data} .  "\n";
                    127:                        print "\tNotes: " . $a->{255}->{data} . "\n" if $a->{255}->{data};
1.1       andrew    128:        }
                    129:
                    130: }
                    131:
                    132: sub add_item
                    133: {
                    134:        die "not implemented!";
                    135: }
                    136:
                    137: sub delete_item
                    138: {
                    139:        die "not implemented!";
                    140: }
                    141:
1.2       andrew    142: sub get_password
1.1       andrew    143: {
                    144:        while (1) {
                    145:                print "Enter Password: ";
                    146:
                    147:                system("stty", "-echo");
                    148:                chop(my $read = <STDIN>);
                    149:         system("stty", "echo");
                    150:         print "\n";
                    151:
                    152:                $read =~ s/^\s*//;
                    153:                $read =~ s/\s*$//;
                    154:
                    155:                #return 1 if
1.2       andrew    156:                $pdb->Password($read) && return 1;
1.1       andrew    157:                #print Dump $read, $pdb;
                    158:                #exit;
                    159:        }
                    160:        return undef;
                    161: }
                    162:
                    163:
                    164: sub help
                    165: {
                    166:        print STDERR "$0 [options] action\n";
                    167:        exit 255;
                    168: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>