From 33166c7995959d8d809424ac02772bc7a56696d2 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 31 Jan 2022 01:21:04 -0500 Subject: xdg basedir added. store certs in appropriate dirs --- gmi.pl | 70 +++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/gmi.pl b/gmi.pl index 8b7d595..194ac6b 100755 --- a/gmi.pl +++ b/gmi.pl @@ -10,7 +10,7 @@ use warnings; use feature qw(refaliasing); no warnings qw(experimental::refaliasing); -our $VERSION = 'v0.0.29'; +our $VERSION = 'v0.0.30'; # TODO: # back() only works once; should fix this @@ -30,8 +30,9 @@ use URI::Encode qw(uri_encode); # CPAN use IO::Socket::SSL::Utils; # CPAN use File::Slurper qw(read_dir); # CPAN use Data::Dumper; +use File::BaseDir qw(xdg_config_home xdg_data_home xdg_cache_home); -# sudo cpanm IO::Socket::SSL URL::XS IO::Pager Text::Wraper Term::ReadKey Path::Naive Text::ParseWords Term::ReadLine Smart::Comments URI::Encode IO::Socket::SSL::Utils File::Slurper +# sudo cpanm IO::Socket::SSL URL::XS IO::Pager Text::Wraper Term::ReadKey Path::Naive Text::ParseWords Term::ReadLine Smart::Comments URI::Encode IO::Socket::SSL::Utils File::Slurper File::BaseDir my $wrapper = Text::Wrapper->new(columns=>70, body_start=>''); #$ENV{PAGER} = 'most'; @@ -58,6 +59,23 @@ my $use_cert = 0; my $cert; my $key; my %known_hosts; +my $program_name = 'jakes-gemini-client'; +my $bd = File::BaseDir->new; +my $xdg_config = $bd->xdg_config_home . "/$program_name/"; +my $xdg_data = $bd->xdg_data_home . "/$program_name/"; +my $xdg_cache = $bd->xdg_cache_home . "/$program_name/"; +if (! -e $xdg_config) { + mkdir($xdg_config) +} +if (! -e $xdg_data) { + mkdir($xdg_data) +} +if (! -e $xdg_cache) { + mkdir($xdg_cache) +} +### $xdg_config +### $xdg_data +### $xdg_cache my $term=new Term::ReadLine "hmm, what goes here?"; my $OUT = $term->OUT || \*STDOUT; @@ -113,7 +131,8 @@ my %commands = ( "automatically determine the appropriate length."], 'pager' => [sub { pager($_[0]) }, "Set which pager to use. (currently $ENV{PAGER})"], 'save' => [sub { ; }, 'Save the config settings. (Not yet implimented)'], - 'help' => [sub { help($_[0]); }, 'Use help before a command to get extra info. (cmds to see commands)'], + 'help' => [sub { help($_[0]); }, 'Use help before a command to get extra info.' . + '( to see commands)'], 'ver' => [sub {print $OUT "$VERSION\n"}, "Returns the version ($VERSION)"], 'exit' => [sub {exit 0;}, "This exits the program with a status code of 0."], @@ -927,24 +946,21 @@ sub clear_query { } sub cert_create { - # this should create certs somewhere more optimal, rather than the working directory - my $old_umask = umask(077); - if (! -e './certs') { - mkdir('./certs'); + if (! -e "$xdg_data/certs") { + mkdir("$xdg_data/certs"); } print $OUT "What should the profile name be? "; chomp(my $input = ); $input =~ tr|/|.|; - if (-e "./certs/$input") { + if (-e "$xdg_data/certs/$input") { print $OUT "Sorry, but that already exists.\n"; } else { - mkdir("./certs/$input"); + mkdir("$xdg_data/certs/$input"); print $OUT "What should the commonName be? (it will be used to identify you easily) "; chomp(my $common_name = ); - #print $OUT "When should this expire (in seconds ('31536000' = 1 year))? "; my $expire; while (1) { print $OUT "When should this expire? (1s, 1h, 1d, 1m, 1y, 0s (cancel)) "; @@ -987,8 +1003,8 @@ sub cert_create { 'not_after' => $expire, ); my ($cert, $key) = CERT_create(%cert); - PEM_cert2file($cert,"./certs/$input/cert.pem"); - PEM_key2file($key,"./certs/$input/key.pem"); + PEM_cert2file($cert,"$xdg_data/certs/$input/cert.pem"); + PEM_key2file($key,"$xdg_data/certs/$input/key.pem"); CERT_free($cert); KEY_free($key); } @@ -1000,12 +1016,11 @@ sub cert_use { my $counter = 1; my @profiles; - # this should read dirs from a more optimal location, rather than working directory - if (! -e './certs') { + if (! -e "$xdg_data/certs") { print $OUT "You need to create a cert first: 'cert-create'.\n"; return 1; } - for (read_dir('./certs')) { + for (read_dir("$xdg_data/certs")) { print $OUT "[$counter] $_\n"; push(@profiles, "$_"); $counter++; @@ -1019,14 +1034,13 @@ sub cert_use { chomp(my $input = ); if ($input) { - print $OUT "Loading 'certs/$profiles[$input-1]/cert.pem' and 'certs/$profiles[$input-1]/key.pem'\n"; eval { - $cert = PEM_file2cert("certs/$profiles[$input-1]/cert.pem"); - $key = PEM_file2key("certs/$profiles[$input-1]/key.pem"); + $cert = PEM_file2cert("$xdg_data/certs/$profiles[$input-1]/cert.pem"); + $key = PEM_file2key("$xdg_data/certs/$profiles[$input-1]/key.pem"); CERT_free($cert); KEY_free($key); - $cert = "certs/$profiles[$input-1]/cert.pem"; - $key = "certs/$profiles[$input-1]/key.pem"; + $cert = "$xdg_data/certs/$profiles[$input-1]/cert.pem"; + $key = "$xdg_data/certs/$profiles[$input-1]/key.pem"; }; if ($@) { print $OUT "Something is wrong with the certificate/key. Will not use.\n$@"; @@ -1049,31 +1063,31 @@ sub cert_use { } sub cert_del { - # cert dir should be in a more optimal place, not working directory. - if (! -e './certs') { - print $OUT "You have no certs/ directory.\n"; + if (! -e "$xdg_data/certs") { + print $OUT "You need to create a cert first: 'cert-create'.\n"; return 0; } my $counter = 1; my @profiles; - for (read_dir('./certs')) { + for (read_dir("$xdg_data/certs")) { print $OUT "[$counter] $_\n"; push(@profiles, "$_"); $counter++; } if (! @profiles) { print $OUT "You need to create a cert first: 'cert-create'.\n"; - return 1; + return 0; } print $OUT "Delete which profile? "; chomp(my $input = ); if ($input) { - print $OUT "Are you sure you want to delete certs/$profiles[$input-1]/? y/N\n"; + print $OUT "Are you sure you want to delete $profiles[$input-1]/? y/N"; chomp(my $yORn = ); if (lc $yORn eq 'y') { - unlink("./certs/$profiles[$input-1]/cert.pem","./certs/$profiles[$input-1]/key.pem"); - rmdir("./certs/$profiles[$input-1]"); + unlink("$xdg_data/certs/$profiles[$input-1]/cert.pem", + "$xdg_data/certs/$profiles[$input-1]/key.pem"); + rmdir("$xdg_data/certs/$profiles[$input-1]"); if ($!) { print $OUT "Unable to delete profile.\n$!\n"; } -- cgit v1.2.3