Return a dictionary from GPGCheckRecipients

This is a step towards reporting keys that aren't valid for encryption
instead of just ignoring them.

Signed-off-by: James McCoy <vega.james@gmail.com>
This commit is contained in:
James McCoy 2013-01-25 07:49:57 -05:00
parent 7e6816802b
commit 2f1d2c8244

View File

@ -430,7 +430,7 @@ function s:GPGDecrypt(bufread)
let recipient = matchstr(output, s:keyPattern, start) let recipient = matchstr(output, s:keyPattern, start)
call s:GPGDebug(1, "recipient is " . recipient) call s:GPGDebug(1, "recipient is " . recipient)
let name = s:GPGNameToID(recipient) let name = s:GPGNameToID(recipient)
if (strlen(name) > 0) if !empty(name)
let b:GPGRecipients += [name] let b:GPGRecipients += [name]
call s:GPGDebug(1, "name of recipient is " . name) call s:GPGDebug(1, "name of recipient is " . name)
else else
@ -548,7 +548,7 @@ function s:GPGEncrypt()
endif endif
" initialize GPGOptions if not happened before " initialize GPGOptions if not happened before
if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) if (!exists("b:GPGOptions") || empty(b:GPGOptions))
let b:GPGOptions = [] let b:GPGOptions = []
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
let b:GPGOptions += ["symmetric"] let b:GPGOptions += ["symmetric"]
@ -576,10 +576,10 @@ function s:GPGEncrypt()
endif endif
" check here again if all recipients are available in the keyring " check here again if all recipients are available in the keyring
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) let recipients = s:GPGCheckRecipients(b:GPGRecipients)
" check if there are unknown recipients and warn " check if there are unknown recipients and warn
if (len(unknownrecipients) > 0) if !empty(recipients.unknown)
echohl GPGWarning echohl GPGWarning
echom "Please use GPGEditRecipients to correct!!" echom "Please use GPGEditRecipients to correct!!"
echo echo
@ -590,11 +590,7 @@ function s:GPGEncrypt()
endif endif
" built list of recipients " built list of recipients
if (len(recipients) > 0) let options .= ' ' . join(map(recipients.valid, '"-r ".v:val'), ' ')
for gpgid in recipients
let options = options . " -r " . gpgid
endfor
endif
" encrypt the buffer " encrypt the buffer
let destfile = tempname() let destfile = tempname()
@ -640,25 +636,25 @@ function s:GPGViewRecipients()
return return
endif endif
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) let recipients = s:GPGCheckRecipients(b:GPGRecipients)
echo 'This file has following recipients (Unknown recipients have a prepended "!"):' echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
" echo the recipients " echo the recipients
for name in recipients for name in recipients.valid
let name = s:GPGIDToName(name) let name = s:GPGIDToName(name)
echo name echo name
endfor endfor
" echo the unknown recipients " echo the unknown recipients
echohl GPGWarning echohl GPGWarning
for name in unknownrecipients for name in recipients.unknown
let name = "!" . name let name = "!" . name
echo name echo name
endfor endfor
echohl None echohl None
" check if there is any known recipient " check if there is any known recipient
if (len(recipients) == 0) if empty(recipients.valid)
echohl GPGError echohl GPGError
echom 'There are no known recipients!' echom 'There are no known recipients!'
echohl None echohl None
@ -734,12 +730,12 @@ function s:GPGEditRecipients()
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
" get the recipients " get the recipients
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients")) let recipients = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients"))
" if there are no known or unknown recipients, use the default ones " if there are no known or unknown recipients, use the default ones
if (len(recipients) == 0 && len(unknownrecipients) == 0) if (empty(recipients.valid) && empty(recipients.unknown))
if (type(g:GPGDefaultRecipients) == type([])) if (type(g:GPGDefaultRecipients) == type([]))
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients) let recipients = s:GPGCheckRecipients(g:GPGDefaultRecipients)
else else
echohl GPGWarning echohl GPGWarning
echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!" echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!"
@ -748,15 +744,15 @@ function s:GPGEditRecipients()
endif endif
" put the recipients in the scratch buffer " put the recipients in the scratch buffer
for name in recipients for name in recipients.valid
let name = s:GPGIDToName(name) let name = s:GPGIDToName(name)
silent put =name silent put =name
endfor endfor
" put the unknown recipients in the scratch buffer " put the unknown recipients in the scratch buffer
let syntaxPattern = '' let syntaxPattern = ''
if !empty(unknownrecipients) if !empty(recipients.unknown)
let flaggedNames = map(unknownrecipients, '"!".v:val') let flaggedNames = map(recipients.unknown, '"!".v:val')
call append('$', flaggedNames) call append('$', flaggedNames)
let syntaxPattern = '\(' . join(flaggedNames, '\|') . '\)' let syntaxPattern = '\(' . join(flaggedNames, '\|') . '\)'
endif endif
@ -827,9 +823,9 @@ function s:GPGFinishRecipientsBuffer()
let recipient = substitute(recipient, "^GPG:.*$", "", "") let recipient = substitute(recipient, "^GPG:.*$", "", "")
" only do this if the line is not empty " only do this if the line is not empty
if (strlen(recipient) > 0) if !empty(recipient)
let gpgid = s:GPGNameToID(recipient) let gpgid = s:GPGNameToID(recipient)
if (strlen(gpgid) > 0) if !empty(gpgid)
if (match(recipients, gpgid) < 0) if (match(recipients, gpgid) < 0)
let recipients += [gpgid] let recipients += [gpgid]
endif endif
@ -851,7 +847,7 @@ function s:GPGFinishRecipientsBuffer()
call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1) call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1)
" check if there is any known recipient " check if there is any known recipient
if (len(recipients) == 0) if empty(recipients)
echohl GPGError echohl GPGError
echom 'There are no known recipients!' echom 'There are no known recipients!'
echohl None echohl None
@ -1020,7 +1016,7 @@ function s:GPGFinishOptionsBuffer()
let option = substitute(option, "^GPG:.*$", "", "") let option = substitute(option, "^GPG:.*$", "", "")
" only do this if the line is not empty " only do this if the line is not empty
if (strlen(option) > 0 && match(options, option) < 0) if (!empty(option) && match(options, option) < 0)
let options += [option] let options += [option]
endif endif
endfor endfor
@ -1039,24 +1035,23 @@ endfunction
" Function: s:GPGCheckRecipients(tocheck) {{{2 " Function: s:GPGCheckRecipients(tocheck) {{{2
" "
" check if recipients are known " check if recipients are known
" Returns: two lists recipients and unknownrecipients " Returns: dictionary of recipients, {'valid': [], 'unknown': []}
" "
function s:GPGCheckRecipients(tocheck) function s:GPGCheckRecipients(tocheck)
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()") call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()")
let recipients = [] let recipients = {'valid': [], 'unknown': []}
let unknownrecipients = []
if (type(a:tocheck) == type([])) if (type(a:tocheck) == type([]))
for recipient in a:tocheck for recipient in a:tocheck
let gpgid = s:GPGNameToID(recipient) let gpgid = s:GPGNameToID(recipient)
if (strlen(gpgid) > 0) if !empty(gpgid)
if (match(recipients, gpgid) < 0) if (match(recipients.valid, gpgid) < 0)
let recipients += [gpgid] call add(recipients.valid, gpgid)
endif endif
else else
if (match(unknownrecipients, recipient) < 0) if (match(recipients.unknown, recipient) < 0)
let unknownrecipients += [recipient] call add(recipients.unknown, recipient)
echohl GPGWarning echohl GPGWarning
echom "The recipient \"" . recipient . "\" is not in your public keyring!" echom "The recipient \"" . recipient . "\" is not in your public keyring!"
echohl None echohl None
@ -1065,11 +1060,11 @@ function s:GPGCheckRecipients(tocheck)
endfor endfor
endif endif
call s:GPGDebug(2, "recipients are: " . string(recipients)) call s:GPGDebug(2, "recipients are: " . string(recipients.valid))
call s:GPGDebug(2, "unknown recipients are: " . string(unknownrecipients)) call s:GPGDebug(2, "unknown recipients are: " . string(recipients.unknown))
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()") call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()")
return [ recipients, unknownrecipients ] return recipients
endfunction endfunction
" Function: s:GPGNameToID(name) {{{2 " Function: s:GPGNameToID(name) {{{2