Annotation of palm/Palm-Keyring/examples/example3.pl, Revision 1.12
1.1 andrew 1: #!/usr/bin/perl
1.12 ! andrew 2: # $RedRiver: example3.pl,v 1.11 2008/09/17 14:49:50 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) {
1.12 ! andrew 47: push @{ $Names }, @ARGV;
1.1 andrew 48: show_items($Names);
49: } elsif (defined $Categories) {
50: show_categories();
51: } else {
52: help();
53: }
54:
55: exit;
56:
57:
58: sub show_list
59: {
60: print "Showing List\n";
1.5 andrew 61: foreach my $r (@{ $pdb->{records} }) {
1.1 andrew 62: my $category =
1.5 andrew 63: $pdb->{appinfo}->{categories}->[ $r->{category} ]->{name};
1.1 andrew 64:
65: my $matched = 0;
66: foreach my $cat (@{ $Categories }) {
67: $matched++ if uc($category) eq uc($cat);
68: }
69: foreach my $name (@{ $Names}) {
1.3 andrew 70: $matched++ if uc($r->{'name'}) eq uc($name);
1.1 andrew 71: }
72: next if ( @{ $Categories } || @{ $Names } ) && not $matched;
73:
74: # XXX Fix up formatting
1.6 andrew 75: print $r->{plaintext}->{0}->{data} .
1.1 andrew 76: ":" .
1.5 andrew 77: $r->{category} .
1.1 andrew 78: ":" .
79: $category .
80: "\n";
81:
82: }
83: }
84:
85: sub show_categories
86: {
87: foreach my $c (sort @{ $pdb->{'appinfo'}->{'categories'} }) {
88: next unless $c->{'name'};
89: # Fix formatting
90: print $c->{'name'}, "\n";
91: }
92: }
93:
94: sub show_items
95: {
1.10 andrew 96: get_password() || die "Couldn't decrypt file!";
1.2 andrew 97: foreach (0..$#{ $pdb->{'records'} }) {
1.10 andrew 98: my $r = $pdb->{'records'}->[$_];
1.1 andrew 99: my $category =
100: $pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
101:
102: my $matched = 0;
103: foreach my $cat (@{ $Categories }) {
104: $matched++ if uc($category) eq uc($cat);
105: }
106: foreach my $name (@{ $Names}) {
1.6 andrew 107: $matched++ if uc($r->{plaintext}->{0}->{data}) eq uc($name);
1.1 andrew 108: }
109: next if ( @{ $Categories } || @{ $Names } ) && not $matched;
1.9 andrew 110:
1.10 andrew 111: my $a = $pdb->Decrypt($r);
1.8 andrew 112:
1.10 andrew 113: $matched = 0;
1.8 andrew 114: foreach my $account (@{ $Accounts }) {
1.10 andrew 115: $matched++ if uc($a->{1}->{data}) eq uc($account);
1.8 andrew 116: }
117: next if ( @{ $Accounts } ) && not $matched;
1.2 andrew 118:
1.1 andrew 119: # XXX Fix up formatting
1.5 andrew 120: print $a->{0}->{data} . "\n\t" .
1.2 andrew 121: "Category: " . $category . "\n\t" .
1.5 andrew 122: "Account: " . $a->{1}->{data} . "\n\t" .
123: "Password: " . $a->{2}->{data} . "\n";
124: print "\tNotes: " . $a->{255}->{data} . "\n" if $a->{255}->{data};
1.1 andrew 125: }
126:
127: }
128:
129: sub add_item
130: {
131: die "not implemented!";
132: }
133:
134: sub delete_item
135: {
136: die "not implemented!";
137: }
138:
1.2 andrew 139: sub get_password
1.1 andrew 140: {
141: while (1) {
142: print "Enter Password: ";
143:
144: system("stty", "-echo");
145: chop(my $read = <STDIN>);
146: system("stty", "echo");
147: print "\n";
148:
149: $read =~ s/^\s*//;
150: $read =~ s/\s*$//;
151:
152: #return 1 if
1.2 andrew 153: $pdb->Password($read) && return 1;
1.1 andrew 154: #print Dump $read, $pdb;
155: #exit;
156: }
157: return undef;
158: }
159:
160:
161: sub help
162: {
163: print STDERR "$0 [options] action\n";
164: exit 255;
165: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>