#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
Getopt::Long::Configure('bundling');
use Term::ReadLine;
use YAML;
use lib 'lib';
use Palm::Keyring;
my $Default_File = $ENV{'HOME'} . '/.jpilot/Keys-Gtkr.pdb';
my $PDBFile;
my $Categories;
my $Names;
my $Action_List;
my $Action_Show;
my $result = GetOptions (
"file|f=s" => \$PDBFile,
"categories|c:s@" => \$Categories,
"name|n=s@" => \$Names,
"list|l" => \$Action_List,
"show|s" => \$Action_Show,
);
$PDBFile ||= $Default_File;
my $pdb = new Palm::Keyring();
$pdb->Load($PDBFile) || die "Couldn't load '$PDBFile': $!";
if ($Action_List) {
show_list();
} elsif ($Action_Show) {
show_items($Names);
} elsif (defined $Categories) {
show_categories();
} else {
help();
}
exit;
#$pdb->Decrypt('12345');
#print Dump $pdb;
#--------------------------------------------------
# my $new_record = $pdb->append_Record();
#
# $new_record->{plaintext} = {
# name => 'Test3',
# account => 'anothertestaccount',
# password => 'adifferentmypass',
# description => 'now that really roxorZ!',
# };
#
#$pdb->Encrypt('12345');
#
#$pdb->Write("Keys-GtkrNEW.PDB");
#--------------------------------------------------
sub show_list
{
print "Showing List\n";
foreach my $r (@{ $pdb->{'records'} }) {
next unless $r->{'plaintext'};
my $category =
$pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
my $matched = 0;
foreach my $cat (@{ $Categories }) {
$matched++ if uc($category) eq uc($cat);
}
foreach my $name (@{ $Names}) {
$matched++ if uc($r->{'plaintext'}->{'name'}) eq uc($name);
}
next if ( @{ $Categories } || @{ $Names } ) && not $matched;
# XXX Fix up formatting
print $r->{'plaintext'}->{'name'} .
":" .
$r->{'category'} .
":" .
$category .
"\n";
}
}
sub show_categories
{
foreach my $c (sort @{ $pdb->{'appinfo'}->{'categories'} }) {
next unless $c->{'name'};
# Fix formatting
print $c->{'name'}, "\n";
}
}
sub show_items
{
decrypt() || die "Couldn't decrypt file!";
foreach my $r (@{ $pdb->{'records'} }) {
next unless $r->{'plaintext'};
my $category =
$pdb->{'appinfo'}->{'categories'}->[ $r->{'category'} ]->{'name'};
my $matched = 0;
foreach my $cat (@{ $Categories }) {
$matched++ if uc($category) eq uc($cat);
}
foreach my $name (@{ $Names}) {
$matched++ if uc($r->{'plaintext'}->{'name'}) eq uc($name);
}
next if ( @{ $Categories } || @{ $Names } ) && not $matched;
# XXX Fix up formatting
print $r->{'plaintext'}->{'name'} .
"\n\t" .
"Category: " . $category .
"\n\t" .
"Account: " . $r->{'plaintext'}->{'account'} .
"\n\t" .
"Password: " . $r->{'plaintext'}->{'password'} .
"\n";
print "\tDescription: " . $r->{'plaintext'}->{'description'} . "\n"
if $r->{'plaintext'}->{'description'};
}
}
sub add_item
{
die "not implemented!";
}
sub delete_item
{
die "not implemented!";
}
sub decrypt
{
while (1) {
print "Enter Password: ";
system("stty", "-echo");
chop(my $read = <STDIN>);
system("stty", "echo");
print "\n";
$read =~ s/^\s*//;
$read =~ s/\s*$//;
#return 1 if
$pdb->Decrypt($read) && return 1;
#print Dump $read, $pdb;
#exit;
}
return undef;
}
sub help
{
print STDERR "$0 [options] action\n";
exit 255;
}