From 3831bd402d5adc5c05b8817a623ca87f93c5335c Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Sun, 29 Mar 2015 21:15:54 +0200 Subject: Fix the 'insert' command. --- cli/icevault | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/cli/icevault b/cli/icevault index d9a6792..41fb5d8 100755 --- a/cli/icevault +++ b/cli/icevault @@ -459,10 +459,16 @@ sub getForms() { return @$forms; } -# Guess which form is to be filled: take the first form with a password -# field if there are any, otherwise the first non-empty form. If the -# first argument is defined, only consider forms with a matching base -# URI. +# Guess which form is to be filled: +# * If $myform is defined, consider only the forms with matching action / base +# URI; and if there is a single match take it +# * If one of the considered forms has a password field and a (possibly +# different) non-empty field, take the first one found. +# * Otherwise (if all forms with a password field are empty), and if +# there is a form with a password field, take the first one found. +# * Otherwise (if no form has a password field), and if there +# is a non-empty form, take the first one found. +# * Otherwise (if all forms are empty), take the first form in the list. sub findForm($@) { my $myform = shift; my @forms = @_; @@ -476,8 +482,11 @@ sub findForm($@) { return $formIdx[0] if $#formIdx == 0; # single match } - my $idx = firstIdx { defined $_ and any {$_->{type} eq 'password'} @{$_->{fields}} } @forms; - $idx //= firstIdx { defined $_ and any {$_->{value} ne ''} @{$_->{fields}} } @forms; + my @formIdx = grepIdx { defined $_ and any {$_->{type} eq 'password'} @{$_->{fields}} } @forms; + my $idx = first { any {$_->{value} ne ''} @{$forms[$_]->{fields}} } @formIdx; # first non-empty form with a password + $idx //= $formIdx[0]; # first form with a password + $idx //= firstIdx { defined $_ and any {$_->{value} ne ''} @{$_->{fields}} } @forms; # first non-empty form + $idx //= 0 if @forms; # first form error 'Dunno which form to '. (defined $myform ? 'fill' : 'import') unless defined $idx; return $idx; } @@ -777,7 +786,7 @@ elsif ($COMMAND eq 'insert') { my $pwIdx = firstIdx { $_->{type} eq 'password' } @{$form->{fields}}; my $idx = first { $form->{fields}->[$_]->{value} ne '' and ($form->{fields}->[$_]->{type} eq 'text' or $form->{fields}->[$_]->{type} eq 'email') - } ($pwIdx-1 .. 0) + } reverse (0 .. $pwIdx-1) if defined $pwIdx; $idx //= firstIdx { $_->{value} ne '' and ($_->{type} eq 'text' or $_->{type} eq 'email') } @{$form->{fields}}; $id = $form->{fields}->[$idx]->{value} if defined $idx; @@ -804,8 +813,10 @@ elsif ($COMMAND eq 'insert') { my @dontsave; if ($#passIdx == 0) { # single password in the form if ($form->{fields}->[$passIdx[0]]->{value} eq '') { - warning "Warning! Empty password for field C<%s>", $form->{fields}->[$passIdx[0]]->{name}; - exit 1 unless promptYN "Continue?"; + exit 1 unless promptYN "Empty password for field C<%s>. Generate and continue?", 0, + $form->{fields}->[$passIdx[0]]->{name}; + pwgen $form->{fields}->[$passIdx[0]]; + $fill[$passIdx[0]] = $form->{fields}->[$passIdx[0]]->{value}; } } elsif ($#passIdx < 3) { # 2 or 3 passwords in the form -- cgit v1.2.3