[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.4

1.1       andrew      1: #!/usr/bin/perl
1.4     ! andrew      2: # $RedRiver$
        !             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;
                     29: my $Action_List;
                     30: my $Action_Show;
                     31:
                     32: my $result = GetOptions (
                     33:        "file|f=s"        => \$PDBFile,
                     34:        "categories|c:s@" => \$Categories,
                     35:        "name|n=s@"       => \$Names,
                     36:        "list|l"          => \$Action_List,
                     37:        "show|s"          => \$Action_Show,
                     38: );
                     39:
                     40: $PDBFile ||= $Default_File;
1.2       andrew     41: my $pdb = new Palm::PDB();
1.1       andrew     42: $pdb->Load($PDBFile) || die "Couldn't load '$PDBFile': $!";
                     43:
                     44: if ($Action_List) {
                     45:        show_list();
                     46: } elsif ($Action_Show) {
                     47:        show_items($Names);
                     48: } elsif (defined $Categories) {
                     49:        show_categories();
                     50: } else {
                     51:        help();
                     52: }
                     53:
                     54: exit;
                     55:
                     56:
                     57: sub show_list
                     58: {
                     59:        print "Showing List\n";
1.2       andrew     60:        foreach (0..$#{ $pdb->{'records'} }) {
                     61:         next if $_ == 0;
                     62:         my $r = $pdb->{'records'}->[$_];
1.1       andrew     63:                my $category =
                     64:                        $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
                     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.3       andrew     76:                print $r->{'name'} .
1.1       andrew     77:                        ":" .
                     78:                        $r->{'category'} .
                     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:         next if $_ == 0;
                    101:         my $r = $pdb->{'records'}->[$_];
1.1       andrew    102:
                    103:                my $category =
                    104:                        $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
                    105:
                    106:                my $matched = 0;
                    107:                foreach my $cat (@{ $Categories }) {
                    108:                        $matched++ if uc($category) eq uc($cat);
                    109:                }
                    110:                foreach my $name (@{ $Names}) {
1.3       andrew    111:                        $matched++ if uc($r->{'name'}) eq uc($name);
1.1       andrew    112:                }
                    113:                next if ( @{ $Categories } || @{ $Names } ) && not $matched;
                    114:
1.2       andrew    115:         my $a = $pdb->Decrypt($r);
                    116:
1.1       andrew    117:                # XXX Fix up formatting
1.3       andrew    118:                print $r->{'name'} .  "\n\t" .
1.2       andrew    119:                        "Category: " . $category .  "\n\t" .
                    120:                        "Account:  " . $a->{'account'} .  "\n\t" .
                    121:                        "Password: " . $a->{'password'} .  "\n";
                    122:                        print "\tNotes: " . $a->{'notes'} . "\n" if $a->{'notes'};
1.1       andrew    123:        }
                    124:
                    125: }
                    126:
                    127: sub add_item
                    128: {
                    129:        die "not implemented!";
                    130: }
                    131:
                    132: sub delete_item
                    133: {
                    134:        die "not implemented!";
                    135: }
                    136:
1.2       andrew    137: sub get_password
1.1       andrew    138: {
                    139:        while (1) {
                    140:                print "Enter Password: ";
                    141:
                    142:                system("stty", "-echo");
                    143:                chop(my $read = <STDIN>);
                    144:         system("stty", "echo");
                    145:         print "\n";
                    146:
                    147:                $read =~ s/^\s*//;
                    148:                $read =~ s/\s*$//;
                    149:
                    150:                #return 1 if
1.2       andrew    151:                $pdb->Password($read) && return 1;
1.1       andrew    152:                #print Dump $read, $pdb;
                    153:                #exit;
                    154:        }
                    155:        return undef;
                    156: }
                    157:
                    158:
                    159: sub help
                    160: {
                    161:        print STDERR "$0 [options] action\n";
                    162:        exit 255;
                    163: }

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