PM/pm.pl

141 lines
3.1 KiB
Perl
Raw Normal View History

2014-04-27 03:46:32 +04:00
#!/usr/bin/perl
use Getopt::Std;
2014-05-12 15:41:05 +04:00
use Term::ANSIColor;
use Password;
2014-04-29 15:45:31 +04:00
use ClipPass;
2014-05-12 15:41:05 +04:00
use Usage;
2014-04-27 03:46:32 +04:00
2014-04-28 11:29:38 +04:00
# Debug
use Data::Dumper;
2014-05-06 15:18:34 +04:00
our $VERSION = '0.0.1-beta1';
2014-04-29 15:45:31 +04:00
2014-05-12 15:41:05 +04:00
my $usage = Usage->new();
2014-04-27 03:46:32 +04:00
sub init() {
2014-05-13 17:57:14 +04:00
my $opt_string = 'swn:l:p:rhvou:i:c:x:';
2014-05-12 15:41:05 +04:00
getopts("$opt_string") or $usage->show();
2014-04-29 15:45:31 +04:00
our (
2014-05-13 17:57:14 +04:00
$opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h,
$opt_v, $opt_o, $opt_u, $opt_i, $opt_c, $opt_x,
2014-04-29 15:45:31 +04:00
);
2014-04-28 12:17:17 +04:00
print "Simple password manager writed in Perl.\nVersion: "
. $VERSION
. "\n" and exit 0
if $opt_v;
2014-05-12 15:41:05 +04:00
$usage->show() if $opt_h;
2014-04-27 03:46:32 +04:00
}
2014-04-28 12:04:17 +04:00
my $pass = Password->new();
2014-04-28 12:17:17 +04:00
2014-05-06 12:18:44 +04:00
if ( $pass->check_config() == 0 ) {
2014-05-06 15:18:34 +04:00
exit 0;
2014-05-06 12:18:44 +04:00
}
2014-04-29 15:45:31 +04:00
2014-05-06 15:01:07 +04:00
init();
2014-05-05 22:37:59 +04:00
2014-04-29 15:45:31 +04:00
my $copy = ClipPass->new();
# Command line arguments switch
# It's really ugly code. Sorry :(
2014-04-29 15:45:31 +04:00
if ( defined($opt_s) and defined($opt_n) and !defined($opt_o) ) {
2014-05-12 11:07:30 +04:00
my $get_h = $pass->show( $opt_n, $opt_u );
2014-05-06 00:40:26 +04:00
my $get_pass = $get_h->{password};
2014-05-06 00:40:26 +04:00
$copy->copy($get_pass);
2014-05-12 11:07:30 +04:00
2014-05-08 00:21:22 +04:00
print color 'green';
print "Password copied to xclipboard.";
print color 'reset';
print "\nURI is ";
print color 'bold blue';
print $get_h->{resource} . "\n";
print color 'reset';
2014-04-29 15:45:31 +04:00
}
elsif ( defined($opt_s) and defined($opt_n) and defined($opt_o) ) {
2014-05-12 11:07:30 +04:00
my $get_h = $pass->show( $opt_n, $opt_u );
2014-05-06 15:18:34 +04:00
$copy->copy( $get_h->{password} );
# Open resource.
2014-05-06 15:18:34 +04:00
my @open_cmd = ( 'xdg-open', $get_h->{resource} );
system(@open_cmd) == 0 or die "Cannot open URI: $!\n";
2014-05-12 15:25:22 +04:00
print color 'bold green';
print "Password copied to clipboard.\n";
print color 'reset';
print "Trying to open ";
print color 'bold blue';
print $get_h->{resource} . "\n";
print color 'reset';
2014-04-29 15:45:31 +04:00
}
2014-05-12 17:16:10 +04:00
2014-05-12 11:07:30 +04:00
# Remove string from db
elsif ( defined($opt_r) and defined($opt_i) ) {
my $store_h = { id => $opt_i, };
2014-05-12 13:46:23 +04:00
$pass->remove($store_h) == 0 or die "Oops! 111: pm.pl. $!\n";
print color 'bold red';
print "Password was removed!\n";
print color 'reset';
2014-05-12 11:07:30 +04:00
}
2014-04-29 15:45:31 +04:00
elsif ( defined($opt_w)
and defined($opt_n)
and defined($opt_l)
and !defined($opt_p) )
{
# Generate password and store it into DB
print "$opt_w, $opt_n, $opt_l, $opt_p\n";
2014-05-05 23:51:31 +04:00
2014-05-12 15:41:05 +04:00
$opt_p = $pass->generate();
2014-05-05 23:51:31 +04:00
my $store_h = {
2014-05-06 15:18:34 +04:00
name => $opt_n,
resource => $opt_l,
2014-05-12 15:41:05 +04:00
password => $opt_p,
2014-05-06 15:18:34 +04:00
username => $opt_u,
2014-05-12 17:16:10 +04:00
comment => $opt_c,
2014-05-05 23:51:31 +04:00
};
$pass->save($store_h) == 0 or die "Oops! 105: pm.pl. $!\n";
2014-05-12 17:16:10 +04:00
$copy->copy($opt_p);
2014-05-12 15:41:05 +04:00
print color 'green';
print "Password was stored into DB!\n";
print color 'reset';
2014-04-29 15:45:31 +04:00
}
elsif ( defined($opt_w)
and defined($opt_n)
and defined($opt_l)
and defined($opt_p) )
{
# Store new password into DB
print "$opt_w, $opt_n, $opt_l, $opt_p\n";
2014-05-05 23:51:31 +04:00
my $store_h = {
2014-05-06 15:18:34 +04:00
name => $opt_n,
resource => $opt_l,
password => $opt_p,
gen => 0,
username => $opt_u,
2014-05-12 17:16:10 +04:00
comment => $opt_c,
2014-05-05 23:51:31 +04:00
};
$pass->save($store_h) == 0 or die "Oops! 122: pm.pl. $!\n";
2014-05-12 15:41:05 +04:00
print color 'green';
print "Password was stored into DB!\n";
print color 'reset';
2014-04-29 15:45:31 +04:00
}
2014-05-13 17:57:14 +04:00
# Export
elsif ( defined($opt_x) ) {
$pass->export($opt_x);
print colored("Dabase stored in $opt_x\n", 'green');
}
2014-04-29 15:45:31 +04:00
else {
2014-05-12 15:41:05 +04:00
$usage->show();
2014-04-29 15:45:31 +04:00
}