From 8bdcd5edd0e4d5edc5d50f66d7fea5ef20ac3530 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Fri, 9 Mar 2018 10:13:55 -0800 Subject: [PATCH] Fix bug where adding the first contact did not create a new contacts file --- lib/Hush/Contact.pm | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/Hush/Contact.pm b/lib/Hush/Contact.pm index 16ebe0e..348d8c0 100644 --- a/lib/Hush/Contact.pm +++ b/lib/Hush/Contact.pm @@ -1,6 +1,7 @@ package Hush::Contact; use strict; use warnings; +use lib 'lib'; use Hush::Util qw/barf is_valid_zaddr/; use Data::Dumper; use File::Spec::Functions; @@ -25,18 +26,23 @@ sub contact { 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; - } + unless (-e $contacts_file) { + # first contact being added, create the file + open my $fh, ">>", $contacts_file or barf "Could not create contacts file $contacts_file ! : $!"; + print $fh ""; + close $fh; + } + + 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 {