Annotation of palm/Palm-Keyring/examples/example3.pl, Revision 1.2
1.1 andrew 1: #!/usr/bin/perl
2: use strict;
3: use warnings;
4: use Getopt::Long;
5: Getopt::Long::Configure('bundling');
6: use Term::ReadLine;
7:
8: use YAML;
9:
1.2 ! andrew 10: use Palm::PDB;
1.1 andrew 11: use Palm::Keyring;
12:
13: my $Default_File = $ENV{'HOME'} . '/.jpilot/Keys-Gtkr.pdb';
14: my $PDBFile;
15: my $Categories;
16: my $Names;
17: my $Action_List;
18: my $Action_Show;
19:
20: my $result = GetOptions (
21: "file|f=s" => \$PDBFile,
22: "categories|c:s@" => \$Categories,
23: "name|n=s@" => \$Names,
24: "list|l" => \$Action_List,
25: "show|s" => \$Action_Show,
26: );
27:
28: $PDBFile ||= $Default_File;
1.2 ! andrew 29: my $pdb = new Palm::PDB();
1.1 andrew 30: $pdb->Load($PDBFile) || die "Couldn't load '$PDBFile': $!";
31:
32: if ($Action_List) {
33: show_list();
34: } elsif ($Action_Show) {
35: show_items($Names);
36: } elsif (defined $Categories) {
37: show_categories();
38: } else {
39: help();
40: }
41:
42: exit;
43:
44:
45: sub show_list
46: {
47: print "Showing List\n";
1.2 ! andrew 48: foreach (0..$#{ $pdb->{'records'} }) {
! 49: next if $_ == 0;
! 50: my $r = $pdb->{'records'}->[$_];
1.1 andrew 51: my $category =
52: $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
53:
54: my $matched = 0;
55: foreach my $cat (@{ $Categories }) {
56: $matched++ if uc($category) eq uc($cat);
57: }
58: foreach my $name (@{ $Names}) {
1.2 ! andrew 59: $matched++ if uc($r->{'data'}) eq uc($name);
1.1 andrew 60: }
61: next if ( @{ $Categories } || @{ $Names } ) && not $matched;
62:
63: # XXX Fix up formatting
1.2 ! andrew 64: print $r->{'data'} .
1.1 andrew 65: ":" .
66: $r->{'category'} .
67: ":" .
68: $category .
69: "\n";
70:
71: }
72: }
73:
74: sub show_categories
75: {
76: foreach my $c (sort @{ $pdb->{'appinfo'}->{'categories'} }) {
77: next unless $c->{'name'};
78: # Fix formatting
79: print $c->{'name'}, "\n";
80: }
81: }
82:
83: sub show_items
84: {
1.2 ! andrew 85: get_password() || die "Couldn't decrypt file!";
! 86:
! 87: foreach (0..$#{ $pdb->{'records'} }) {
! 88: next if $_ == 0;
! 89: my $r = $pdb->{'records'}->[$_];
1.1 andrew 90:
91: my $category =
92: $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
93:
94: my $matched = 0;
95: foreach my $cat (@{ $Categories }) {
96: $matched++ if uc($category) eq uc($cat);
97: }
98: foreach my $name (@{ $Names}) {
1.2 ! andrew 99: $matched++ if uc($r->{'data'}) eq uc($name);
1.1 andrew 100: }
101: next if ( @{ $Categories } || @{ $Names } ) && not $matched;
102:
1.2 ! andrew 103: my $a = $pdb->Decrypt($r);
! 104:
1.1 andrew 105: # XXX Fix up formatting
1.2 ! andrew 106: print $r->{'data'} . "\n\t" .
! 107: "Category: " . $category . "\n\t" .
! 108: "Account: " . $a->{'account'} . "\n\t" .
! 109: "Password: " . $a->{'password'} . "\n";
! 110: print "\tNotes: " . $a->{'notes'} . "\n" if $a->{'notes'};
1.1 andrew 111: }
112:
113: }
114:
115: sub add_item
116: {
117: die "not implemented!";
118: }
119:
120: sub delete_item
121: {
122: die "not implemented!";
123: }
124:
1.2 ! andrew 125: sub get_password
1.1 andrew 126: {
127: while (1) {
128: print "Enter Password: ";
129:
130: system("stty", "-echo");
131: chop(my $read = <STDIN>);
132: system("stty", "echo");
133: print "\n";
134:
135: $read =~ s/^\s*//;
136: $read =~ s/\s*$//;
137:
138: #return 1 if
1.2 ! andrew 139: $pdb->Password($read) && return 1;
1.1 andrew 140: #print Dump $read, $pdb;
141: #exit;
142: }
143: return undef;
144: }
145:
146:
147: sub help
148: {
149: print STDERR "$0 [options] action\n";
150: exit 255;
151: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>