From 085d0a72220d65a8881926eaf0c48147267e15e8 Mon Sep 17 00:00:00 2001 From: "Difrex(Denis Zheleztsov)" Date: Tue, 29 Apr 2014 11:37:41 +0400 Subject: [PATCH] mdo and save/store writed --- Database.pm | 2 +- Password.pm | 43 +++++++++++++++++++++++++++++++++++++++++-- pm.pl | 4 ++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Database.pm b/Database.pm index ef1f99e..72a2784 100644 --- a/Database.pm +++ b/Database.pm @@ -51,7 +51,7 @@ sub mdo { return $q_hash; } elsif ( $type eq 'do' ) { - $dbh->do($q); + $dbh->do($q) or die "$!\n"; return 0; } else { diff --git a/Password.pm b/Password.pm index ca2216e..4c7f753 100644 --- a/Password.pm +++ b/Password.pm @@ -22,8 +22,6 @@ sub new { my $self = { _db => $db, _gpg => $gpg, - _p_save => $p_save, - _p_show => $p_show, }; bless $self, $class; @@ -57,6 +55,47 @@ sub show { return $q_hash; } +# Decrypt base and store new password +sub save { + my ( $self, $store ) = @_; + my $db_class = $self->{_db}; + my $gpg = $self->{_gpg}; + + my $name = $store->{name}; + my $resource = $store->{resource}; + my $password = $store->{password}; + my $generate = $store->{gen}; + + if ( $generate == 1 ) { + $password = Password->generate(); + } + + # Decrypt database + my $dec_db_file = $gpg->decrypt_db(); + my $q + = "insert into passwords(name, resource, password) values($name, $resource, $password)"; + my $mdo_q = { + file => $dec_db_file, + name => $name, + query => $query, + type => 'do', + }; + + $db_class->mdo($mdo_q); + $gpg->encrypt_db($dec_db_file); + + return 0; +} + +# Generate password +sub generate { + my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 ); + my $string; + $string .= $chars[ rand @chars ] for 1 .. 16; + + return $string; +} + # Check configuration. If it doesn't exist create it. sub check_config { my ($self) = @_; diff --git a/pm.pl b/pm.pl index 192f4a5..e71679c 100755 --- a/pm.pl +++ b/pm.pl @@ -3,7 +3,7 @@ use Password; use Getopt::Std; -our $VERSION = '0.0.0d'; +our $VERSION = '0.0.1a'; # Debug use Data::Dumper; @@ -45,7 +45,7 @@ EOF sub init() { my $opt_string = 'swn:l:p:rhv'; getopts("$opt_string") or usage(); - our ( $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h, $opt_v ); + our ( $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h, $opt_v, $opt_o ); print "Simple password manager writed in Perl.\nVersion: " . $VERSION