From 68da24669996349cdade386a295ce5f69a9e6861 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Sat, 25 Nov 2017 23:57:14 -0800 Subject: [PATCH] Start implementing adding contacts on the command-line --- bin/hushlist | 23 +---------------------- lib/Hush/List.pm | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/bin/hushlist b/bin/hushlist index 4effc35..409f9ff 100755 --- a/bin/hushlist +++ b/bin/hushlist @@ -28,7 +28,7 @@ my $list = Hush::List->new($rpc, $options); my $command = shift || help(); my $COMMANDS = { "add" => \&add, - "contact" => \&contact, + "contact" => \&Hush::List::contact, "help" => \&help, "new" => \&new, "remove" => \&remove, @@ -74,27 +74,6 @@ sub usage { print "$0 help for more details :)\n"; } -sub contact { - my $cmd = shift || ''; - my $subcommands = { - "add" => sub { - # add a hush contact, yay - my ($cmd,$name,$zaddr) = @ARGV; - barf Dumper [ $cmd, $name, $zaddr ]; - }, - "rm" => sub { - my ($cmd,$name,$zaddr) = @ARGV; - barf Dumper [ $cmd, $name, $zaddr ]; - }, - }; - my $subcmd = $subcommands->{$cmd}; - if ($subcmd) { - $subcmd->(); - } else { - barf "Invalid hushlist contact subcommand!"; - } -} - sub status { my $name = shift; if ($name) { diff --git a/lib/Hush/List.pm b/lib/Hush/List.pm index 32ae2c0..318cec8 100644 --- a/lib/Hush/List.pm +++ b/lib/Hush/List.pm @@ -8,6 +8,7 @@ use Hush::Util qw/barf/; use File::Slurp; use Hush::Logger qw/debug/; use JSON; +use Data::Dumper; # as per z_sendmany rpc docs my $MAX_RECIPIENTS = 54; @@ -16,6 +17,44 @@ my $HUSHLIST_CONFIG_DIR = $ENV{HUSH_CONFIG_DIR} || catdir($HUSH_CONFIG_DIR, 'lis our $VERSION = 20171031; my $rpc = Hush::RPC->new; +sub contact { + my $cmd = shift || ''; + my $subcommands = { + "add" => sub { + # add a hush contact, yay + my ($cmd,$name,$zaddr) = @ARGV; + #barf Dumper [ $cmd, $name, $zaddr ]; + #TODO: give user ability to choose + my $chain = "hush"; + my $contacts_file = catdir($HUSHLIST_CONFIG_DIR,"$chain-contacts.txt"); + + if (-e $contacts_file) { + my %contacts = read_file( $contacts_file ) =~ /^(z[a-z0-9]+) (.*)$/mgi ; + # TODO: check if zaddr OR nickname exists + if ($contacts{$zaddr}) { + } else { + # TODO: see if this contact exists already in this chain + open my $fh, ">>", $contacts_file or barf "Could not write file $contacts_file ! : $!"; + #TODO: validation? + print $fh "$zaddr $name\n"; + close $fh; + } + } + }, + "rm" => sub { + my ($cmd,$name,$zaddr) = @ARGV; + barf Dumper [ $cmd, $name, $zaddr ]; + }, + }; + my $subcmd = $subcommands->{$cmd}; + if ($subcmd) { + $subcmd->(); + } else { + barf "Invalid hushlist contact subcommand!"; + } +} + + sub _sanity_checks { if (!-e $HUSH_CONFIG_DIR ) { barf "Hush config directory $HUSH_CONFIG_DIR not found! You can set the HUSH_CONFIG_DIR environment variable if it is not ~/.hush";