Start working on GPG module

This commit is contained in:
Difrex(Denis Zheleztsov) 2014-04-28 12:17:17 +04:00
parent 2423a6d942
commit dcb70474e3
3 changed files with 75 additions and 67 deletions

View File

@ -6,11 +6,14 @@ use GPG;
sub new { sub new {
my $class = shift; my $class = shift;
# Get home dir # Get home dir
my $home = $ENV->{HOME}; my $home = $ENV->{HOME};
my $self = { my $gpg = GPG->new();
_home => $home,
my $self = {
_home => $home,
_gpg => $gpg,
}; };
bless $self, $class; bless $self, $class;
@ -18,50 +21,53 @@ sub new {
} }
sub connect { sub connect {
my ($self) = @_; my ($self) = @_;
my $home = $self->{_home}; my $home = $self->{_home};
my $db_file = $home . "/.PM/db.sqlite"; my $db_file = $home . "/.PM/db.sqlite";
my $dbh = DBI->connect("dbi:SQLite:dbname=$db_file","",""); my $dbh = DBI->connect( "dbi:SQLite:dbname=$db_file", "", "" );
return $dbh; return $dbh;
} }
sub mdo { sub mdo {
my ($self, $query, $type) = @_; my ( $self, $query, $type ) = @_;
my $dbh = $self->{_dbh}; my $dbh = $self->{_dbh};
} }
sub create_base { sub create_base {
my ($self) = @_; my ($self) = @_;
my $home = $self->{_home}; my $home = $self->{_home};
my $pm_dir = $home."/.PM/"; my $pm_dir = $home . "/.PM/";
# Check dir
if (!(-d $pm_dir)) {
# Create dirrectory
@mkdir_cmd = ("mkdir", "$pm_dir");
system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
# Create DB file # Check dir
@createdb_cmd = ("touch", "$pm_dir/db.sqlite"); if ( !( -d $pm_dir ) ) {
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
# Create table. TODO: write this # Create dirrectory
my $dbh = DBI->connect("dbi:SQLite:dbname=$pm_dir/db.sqlite","",""); my @mkdir_cmd = ( "mkdir", "$pm_dir" );
print "Create database schema\n"; system(@mkdir_cmd) == 0 or die "Cannot create dir $pm_dir: $!\n";
my $q_table = "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
$dbh->do($q_table);
# Encrypt db # Create DB file
# TODO: write this my @createdb_cmd = ( "touch", "$pm_dir/db.sqlite" );
system(@createdb_cmd) == 0 or die "Cannot create database file: $!\n";
return 0; # Create table. TODO: write this
} my $dbh
else { = DBI->connect( "dbi:SQLite:dbname=$pm_dir/db.sqlite", "", "" );
print "Dirrectory is exist!\n"; print "Create database schema\n";
return 0; my $q_table
} = "create table passwords(name VARCHAR(32), resource TEXT, password TEXT)";
$dbh->do($q_table);
# Encrypt db
# TODO: write this
return 0;
}
else {
print "Dirrectory is exist!\n";
return 0;
}
} }
1; 1;

View File

@ -10,34 +10,34 @@ use PasswordSave;
use PasswordShow; use PasswordShow;
sub new { sub new {
my $class = shift; my $class = shift;
my $db = Database->new(); my $db = Database->new();
my $p_save = PasswordSave->new(); my $p_save = PasswordSave->new();
my $p_show = PasswordShow->new(); my $p_show = PasswordShow->new();
my $self = { my $self = {
_db => $db, _db => $db,
_p_save => $p_save, _p_save => $p_save,
_p_show => $p_show, _p_show => $p_show,
}; };
bless $self, $class; bless $self, $class;
return $self; return $self;
} }
# Check configuration. If it doesn't exist create it. # Check configuration. If it doesn't exist create it.
sub check_config { sub check_config {
my ($self) = @_; my ($self) = @_;
if (-e $ENV->{HOME}."/.PM/db.sqlite") { if ( -e $ENV->{HOME} . "/.PM/db.sqlite" ) {
return 0; return 0;
} }
else { else {
my $db = $self->{_db}; my $db = $self->{_db};
$db->create_base(); $db->create_base();
} }
return 0; return 0;
} }
1; 1;

24
pm.pl
View File

@ -9,7 +9,7 @@ our $VERSION = '0.0.0b';
use Data::Dumper; use Data::Dumper;
sub usage() { sub usage() {
print STDERR << "EOF"; print STDERR << "EOF";
Simple password manager writed in Perl. Simple password manager writed in Perl.
Usage: Usage:
@ -38,23 +38,25 @@ Examples:
\tPassword copied to clipboard. Trying to open uri. \tPassword copied to clipboard. Trying to open uri.
EOF EOF
exit 1; exit 1;
} }
sub init() { sub init() {
my $opt_string = 'swn:l:p:rhv'; my $opt_string = 'swn:l:p:rhv';
getopts("$opt_string") or usage(); getopts("$opt_string") or usage();
our ($opt_s, $opt_w, $opt_n, $opt_r, our ( $opt_s, $opt_w, $opt_n, $opt_r, $opt_l, $opt_p, $opt_h, $opt_v );
$opt_l, $opt_p, $opt_h, $opt_v);
print "Simple password manager writed in Perl.\nVersion: "
print "Simple password manager writed in Perl.\nVersion: ". . $VERSION
$VERSION."\n" and exit 0 if $opt_v; . "\n" and exit 0
usage if $opt_h; if $opt_v;
usage if $opt_h;
} }
# Parse cmd line # Parse cmd line
init(); init();
my $pass = Password->new(); my $pass = Password->new();
# Don't use it's before GPG and Database # Don't use it's before GPG and Database
# $pass->check_config() == 0 or die "$!\n"; # $pass->check_config() == 0 or die "$!\n";