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

1.1       andrew      1: #!/usr/bin/perl
1.7     ! andrew      2: # $RedRiver: example3.pl,v 1.6 2007/09/12 03:59:37 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.6       andrew     74:                print $r->{plaintext}->{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:         my $r = $pdb->{'records'}->[$_];
1.1       andrew     99:
                    100:                my $category =
                    101:                        $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
                    102:
                    103:                my $matched = 0;
                    104:                foreach my $cat (@{ $Categories }) {
                    105:                        $matched++ if uc($category) eq uc($cat);
                    106:                }
                    107:                foreach my $name (@{ $Names}) {
1.6       andrew    108:                        $matched++ if uc($r->{plaintext}->{0}->{data}) eq uc($name);
1.1       andrew    109:                }
                    110:                next if ( @{ $Categories } || @{ $Names } ) && not $matched;
                    111:
1.2       andrew    112:         my $a = $pdb->Decrypt($r);
                    113:
1.1       andrew    114:                # XXX Fix up formatting
1.5       andrew    115:                print $a->{0}->{data} .  "\n\t" .
1.2       andrew    116:                        "Category: " . $category .  "\n\t" .
1.5       andrew    117:                        "Account:  " . $a->{1}->{data} .  "\n\t" .
                    118:                        "Password: " . $a->{2}->{data} .  "\n";
                    119:                        print "\tNotes: " . $a->{255}->{data} . "\n" if $a->{255}->{data};
1.1       andrew    120:        }
                    121:
                    122: }
                    123:
                    124: sub add_item
                    125: {
                    126:        die "not implemented!";
                    127: }
                    128:
                    129: sub delete_item
                    130: {
                    131:        die "not implemented!";
                    132: }
                    133:
1.2       andrew    134: sub get_password
1.1       andrew    135: {
                    136:        while (1) {
                    137:                print "Enter Password: ";
                    138:
                    139:                system("stty", "-echo");
                    140:                chop(my $read = <STDIN>);
                    141:         system("stty", "echo");
                    142:         print "\n";
                    143:
                    144:                $read =~ s/^\s*//;
                    145:                $read =~ s/\s*$//;
                    146:
                    147:                #return 1 if
1.2       andrew    148:                $pdb->Password($read) && return 1;
1.1       andrew    149:                #print Dump $read, $pdb;
                    150:                #exit;
                    151:        }
                    152:        return undef;
                    153: }
                    154:
                    155:
                    156: sub help
                    157: {
                    158:        print STDERR "$0 [options] action\n";
                    159:        exit 255;
                    160: }

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