Annotation of palm/Palm-Keyring/examples/example3.pl, Revision 1.11
1.1 andrew 1: #!/usr/bin/perl
1.11 ! andrew 2: # $RedRiver: example3.pl,v 1.10 2008/03/05 22:04:46 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:
1.2 andrew 20: use Palm::PDB;
1.1 andrew 21: use Palm::Keyring;
22:
23: my $Default_File = $ENV{'HOME'} . '/.jpilot/Keys-Gtkr.pdb';
24: my $PDBFile;
25: my $Categories;
26: my $Names;
1.8 andrew 27: my $Accounts;
1.1 andrew 28: my $Action_List;
29: my $Action_Show;
30:
31: my $result = GetOptions (
32: "file|f=s" => \$PDBFile,
33: "categories|c:s@" => \$Categories,
34: "name|n=s@" => \$Names,
1.8 andrew 35: "account|a=s@" => \$Accounts,
1.1 andrew 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.10 andrew 95: get_password() || die "Couldn't decrypt file!";
1.2 andrew 96: foreach (0..$#{ $pdb->{'records'} }) {
1.10 andrew 97: my $r = $pdb->{'records'}->[$_];
1.1 andrew 98: my $category =
99: $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
100:
101: my $matched = 0;
102: foreach my $cat (@{ $Categories }) {
103: $matched++ if uc($category) eq uc($cat);
104: }
105: foreach my $name (@{ $Names}) {
1.6 andrew 106: $matched++ if uc($r->{plaintext}->{0}->{data}) eq uc($name);
1.1 andrew 107: }
108: next if ( @{ $Categories } || @{ $Names } ) && not $matched;
1.9 andrew 109:
1.10 andrew 110: my $a = $pdb->Decrypt($r);
1.8 andrew 111:
1.10 andrew 112: $matched = 0;
1.8 andrew 113: foreach my $account (@{ $Accounts }) {
1.10 andrew 114: $matched++ if uc($a->{1}->{data}) eq uc($account);
1.8 andrew 115: }
116: next if ( @{ $Accounts } ) && not $matched;
1.2 andrew 117:
1.1 andrew 118: # XXX Fix up formatting
1.5 andrew 119: print $a->{0}->{data} . "\n\t" .
1.2 andrew 120: "Category: " . $category . "\n\t" .
1.5 andrew 121: "Account: " . $a->{1}->{data} . "\n\t" .
122: "Password: " . $a->{2}->{data} . "\n";
123: print "\tNotes: " . $a->{255}->{data} . "\n" if $a->{255}->{data};
1.1 andrew 124: }
125:
126: }
127:
128: sub add_item
129: {
130: die "not implemented!";
131: }
132:
133: sub delete_item
134: {
135: die "not implemented!";
136: }
137:
1.2 andrew 138: sub get_password
1.1 andrew 139: {
140: while (1) {
141: print "Enter Password: ";
142:
143: system("stty", "-echo");
144: chop(my $read = <STDIN>);
145: system("stty", "echo");
146: print "\n";
147:
148: $read =~ s/^\s*//;
149: $read =~ s/\s*$//;
150:
151: #return 1 if
1.2 andrew 152: $pdb->Password($read) && return 1;
1.1 andrew 153: #print Dump $read, $pdb;
154: #exit;
155: }
156: return undef;
157: }
158:
159:
160: sub help
161: {
162: print STDERR "$0 [options] action\n";
163: exit 255;
164: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>