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

1.1       andrew      1: #!/usr/bin/perl
1.5     ! andrew      2: # $RedRiver: example3.pl,v 1.4 2007/02/10 16:24:16 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;
                     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.5     ! andrew     60:        foreach my $r (@{ $pdb->{records} }) {
1.1       andrew     61:                my $category =
1.5     ! andrew     62:                        $pdb->{appinfo}->{categories}->[ $r->{category} ]->{name};
1.1       andrew     63:
                     64:                my $matched = 0;
                     65:                foreach my $cat (@{ $Categories }) {
                     66:                        $matched++ if uc($category) eq uc($cat);
                     67:                }
                     68:                foreach my $name (@{ $Names}) {
1.3       andrew     69:                        $matched++ if uc($r->{'name'}) eq uc($name);
1.1       andrew     70:                }
                     71:                next if ( @{ $Categories } || @{ $Names } ) && not $matched;
                     72:
                     73:                # XXX Fix up formatting
1.5     ! andrew     74:                print $r->{decrypted}->{0}->{data} .
1.1       andrew     75:                        ":" .
1.5     ! andrew     76:                        $r->{category} .
1.1       andrew     77:                        ":" .
                     78:                        $category .
                     79:                        "\n";
                     80:
                     81:        }
                     82: }
                     83:
                     84: sub show_categories
                     85: {
                     86:        foreach my $c (sort @{ $pdb->{'appinfo'}->{'categories'} }) {
                     87:                next unless $c->{'name'};
                     88:                # Fix formatting
                     89:                print $c->{'name'}, "\n";
                     90:        }
                     91: }
                     92:
                     93: sub show_items
                     94: {
1.2       andrew     95:        get_password() || die "Couldn't decrypt file!";
                     96:
                     97:        foreach (0..$#{ $pdb->{'records'} }) {
                     98:         next if $_ == 0;
                     99:         my $r = $pdb->{'records'}->[$_];
1.1       andrew    100:
                    101:                my $category =
                    102:                        $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
                    103:
                    104:                my $matched = 0;
                    105:                foreach my $cat (@{ $Categories }) {
                    106:                        $matched++ if uc($category) eq uc($cat);
                    107:                }
                    108:                foreach my $name (@{ $Names}) {
1.5     ! andrew    109:                        $matched++ if uc($r->{decrypted}->{0}->{data}) eq uc($name);
1.1       andrew    110:                }
                    111:                next if ( @{ $Categories } || @{ $Names } ) && not $matched;
                    112:
1.2       andrew    113:         my $a = $pdb->Decrypt($r);
                    114:
1.1       andrew    115:                # XXX Fix up formatting
1.5     ! andrew    116:                print $a->{0}->{data} .  "\n\t" .
1.2       andrew    117:                        "Category: " . $category .  "\n\t" .
1.5     ! andrew    118:                        "Account:  " . $a->{1}->{data} .  "\n\t" .
        !           119:                        "Password: " . $a->{2}->{data} .  "\n";
        !           120:                        print "\tNotes: " . $a->{255}->{data} . "\n" if $a->{255}->{data};
1.1       andrew    121:        }
                    122:
                    123: }
                    124:
                    125: sub add_item
                    126: {
                    127:        die "not implemented!";
                    128: }
                    129:
                    130: sub delete_item
                    131: {
                    132:        die "not implemented!";
                    133: }
                    134:
1.2       andrew    135: sub get_password
1.1       andrew    136: {
                    137:        while (1) {
                    138:                print "Enter Password: ";
                    139:
                    140:                system("stty", "-echo");
                    141:                chop(my $read = <STDIN>);
                    142:         system("stty", "echo");
                    143:         print "\n";
                    144:
                    145:                $read =~ s/^\s*//;
                    146:                $read =~ s/\s*$//;
                    147:
                    148:                #return 1 if
1.2       andrew    149:                $pdb->Password($read) && return 1;
1.1       andrew    150:                #print Dump $read, $pdb;
                    151:                #exit;
                    152:        }
                    153:        return undef;
                    154: }
                    155:
                    156:
                    157: sub help
                    158: {
                    159:        print STDERR "$0 [options] action\n";
                    160:        exit 255;
                    161: }

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