releasing gnupg.vim 2276

- added support for default recipients via the variable g:GPGDefaultRecipients.
- fixed an wrong error message with symmetric encryption and set recipients (thanks to Sebastian Luettich).
- create a empty new buffer on leaving vim to wipe out sensitive data on console.
- make sure senisitive data is never written unencrypted to disk.
This commit is contained in:
Markus Braun 2008-08-15 12:50:33 -04:00 committed by James Vega
parent 4b5db78267
commit 7fd5c92bce

View File

@ -1,5 +1,5 @@
" Name: gnupg.vim " Name: gnupg.vim
" Version: $Id: gnupg.vim 2249 2008-07-31 11:43:14Z mbr $ " Version: $Id: gnupg.vim 2276 2008-08-15 12:50:33Z mbr $
" Author: Markus Braun <markus.braun@krawel.de> " Author: Markus Braun <markus.braun@krawel.de>
" Summary: Vim plugin for transparent editing of gpg encrypted files. " Summary: Vim plugin for transparent editing of gpg encrypted files.
" Licence: This program is free software; you can redistribute it and/or " Licence: This program is free software; you can redistribute it and/or
@ -68,6 +68,10 @@
" g:GPGPreferArmor " g:GPGPreferArmor
" If set to 1 armored data is preferred for new files. Defaults to 0. " If set to 1 armored data is preferred for new files. Defaults to 0.
" "
" g:GPGDefaultRecipients
" If set, these recipients are used as defaults when no other recipient is
" defined. This variable is a Vim list. Default is unset.
"
" Credits: " Credits:
" - Mathieu Clabaut for inspirations through his vimspell.vim script. " - Mathieu Clabaut for inspirations through his vimspell.vim script.
" - Richard Bronosky for patch to enable ".pgp" suffix. " - Richard Bronosky for patch to enable ".pgp" suffix.
@ -78,9 +82,11 @@
" - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model " - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model
" and patient beta testing. " and patient beta testing.
" - Giel van Schijndel for patch to get GPG_TTY dynamically. " - Giel van Schijndel for patch to get GPG_TTY dynamically.
" - Sebastian Luettich for patch to fix issue with symmetric encryption an set
" recipients.
" "
" Section: Plugin header {{{1 " Section: Plugin header {{{1
if v:version < 700 if (v:version < 700)
echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None
finish finish
endif endif
@ -89,7 +95,7 @@ if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")
finish finish
endif endif
let g:loaded_gnupg = "$Revision: 2249 $" let g:loaded_gnupg = "$Revision: 2276 $"
" Section: Autocmd setup {{{1 " Section: Autocmd setup {{{1
augroup GnuPG augroup GnuPG
@ -108,6 +114,9 @@ augroup GnuPG
" undo the encryption so we are back in the normal text, directly " undo the encryption so we are back in the normal text, directly
" after the file has been written. " after the file has been written.
autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost() autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost()
" cleanup on leaving vim
autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup()
augroup END augroup END
" Section: Highlight setup {{{1 " Section: Highlight setup {{{1
@ -123,7 +132,7 @@ highlight default link GPGHighlightUnknownRecipient ErrorMsg
function s:GPGInit() function s:GPGInit()
" first make sure nothing is written to ~/.viminfo while editing " first make sure nothing is written to ~/.viminfo while editing
" an encrypted file. " an encrypted file.
set viminfo= set viminfo=
" we don't want a swap file, as it writes unencrypted data to disk " we don't want a swap file, as it writes unencrypted data to disk
set noswapfile set noswapfile
@ -148,6 +157,11 @@ function s:GPGInit()
let g:GPGPreferArmor = 0 let g:GPGPreferArmor = 0
endif endif
" check if debugging is turned on
if (!exists("g:GPGDefaultRecipients"))
let g:GPGDefaultRecipients = []
endif
" check if debugging is turned on " check if debugging is turned on
if (!exists("g:GPGDebugLevel")) if (!exists("g:GPGDebugLevel"))
let g:GPGDebugLevel = 0 let g:GPGDebugLevel = 0
@ -168,21 +182,21 @@ function s:GPGInit()
echohl None echohl None
endif endif
endif endif
let s:GPGCommand=g:GPGExecutable . " --use-agent" let s:GPGCommand = g:GPGExecutable . " --use-agent"
else else
let s:GPGCommand=g:GPGExecutable . " --no-use-agent" let s:GPGCommand = g:GPGExecutable . " --no-use-agent"
endif endif
" don't use tty in gvim " don't use tty in gvim
" FIXME find a better way to avoid an error. " FIXME find a better way to avoid an error.
" with this solution only --use-agent will work " with this solution only --use-agent will work
if has("gui_running") if (has("gui_running"))
let s:GPGCommand=s:GPGCommand . " --no-tty" let s:GPGCommand = s:GPGCommand . " --no-tty"
endif endif
" setup shell environment for unix and windows " setup shell environment for unix and windows
let s:shellredirsave=&shellredir let s:shellredirsave = &shellredir
let s:shellsave=&shell let s:shellsave = &shell
if (match(&shell,"\\(cmd\\|command\\).execute") >= 0) if (match(&shell,"\\(cmd\\|command\\).execute") >= 0)
" windows specific settings " windows specific settings
let s:shellredir = '>%s' let s:shellredir = '>%s'
@ -192,21 +206,31 @@ function s:GPGInit()
" unix specific settings " unix specific settings
let s:shellredir = &shellredir let s:shellredir = &shellredir
let s:shell = 'sh' let s:shell = 'sh'
let s:stderrredirnull ='2>/dev/null' let s:stderrredirnull = '2>/dev/null'
let s:GPGCommand="LANG=C LC_ALL=C " . s:GPGCommand let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand
endif endif
" find the supported algorithms " find the supported algorithms
let &shellredir=s:shellredir let &shellredir = s:shellredir
let &shell=s:shell let &shell = s:shell
let output=system(s:GPGCommand . " --version") let output = system(s:GPGCommand . " --version")
let &shellredir=s:shellredirsave let &shellredir = s:shellredirsave
let &shell=s:shellsave let &shell = s:shellsave
let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "")
let s:GPGCipher=substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "")
let s:GPGHash=substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGHash = substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "")
let s:GPGCompress=substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "") let s:GPGCompress = substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "")
endfunction
" Function: s:GPGCleanup() {{{2
"
" cleanup on leaving vim
"
function s:GPGCleanup()
" wipe out screen
new +only
redraw!
endfunction endfunction
" Function: s:GPGDecrypt() {{{2 " Function: s:GPGDecrypt() {{{2
@ -218,34 +242,34 @@ function s:GPGDecrypt()
set bin set bin
" get the filename of the current buffer " get the filename of the current buffer
let filename=escape(expand("%:p"), '\"') let filename = escape(expand("%:p"), '\"')
" clear GPGEncrypted, GPGRecipients and GPGOptions " clear GPGEncrypted, GPGRecipients and GPGOptions
let b:GPGEncrypted=0 let b:GPGEncrypted = 0
let b:GPGRecipients=[] let b:GPGRecipients = []
let b:GPGOptions=[] let b:GPGOptions = []
" find the recipients of the file " find the recipients of the file
let &shellredir=s:shellredir let &shellredir = s:shellredir
let &shell=s:shell let &shell = s:shell
let output=system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"") let output = system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"")
let &shellredir=s:shellredirsave let &shellredir = s:shellredirsave
let &shell=s:shellsave let &shell = s:shellsave
call s:GPGDebug(1, "output of command '" . s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"' is:") call s:GPGDebug(1, "output of command '" . s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"' is:")
call s:GPGDebug(1, ">>>>> " . output . " <<<<<") call s:GPGDebug(1, ">>>>> " . output . " <<<<<")
" check if the file is symmetric/asymmetric encrypted " check if the file is symmetric/asymmetric encrypted
if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0)
" file is symmetric encrypted " file is symmetric encrypted
let b:GPGEncrypted=1 let b:GPGEncrypted = 1
call s:GPGDebug(1, "this file is symmetric encrypted") call s:GPGDebug(1, "this file is symmetric encrypted")
let b:GPGOptions+=["symmetric"] let b:GPGOptions += ["symmetric"]
" find the used cipher algorithm " find the used cipher algorithm
let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") let cipher = substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "")
if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0)
let b:GPGOptions+=["cipher-algo " . cipher] let b:GPGOptions += ["cipher-algo " . cipher]
call s:GPGDebug(1, "cipher-algo is " . cipher) call s:GPGDebug(1, "cipher-algo is " . cipher)
else else
echohl GPGWarning echohl GPGWarning
@ -255,32 +279,32 @@ function s:GPGDecrypt()
endif endif
elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0) elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0)
" file is asymmetric encrypted " file is asymmetric encrypted
let b:GPGEncrypted=1 let b:GPGEncrypted = 1
call s:GPGDebug(1, "this file is asymmetric encrypted") call s:GPGDebug(1, "this file is asymmetric encrypted")
let b:GPGOptions+=["encrypt"] let b:GPGOptions += ["encrypt"]
" find the used public keys " find the used public keys
let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}") let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}")
while (start >= 0) while (start >= 0)
let start=start + strlen("gpg: public key is ") let start = start + strlen("gpg: public key is ")
let recipient=strpart(output, start, 8) let recipient = strpart(output, start, 8)
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 (strlen(name) > 0)
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
let b:GPGRecipients+=[recipient] let b:GPGRecipients += [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
end end
let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}", start)
endwhile endwhile
else else
" file is not encrypted " file is not encrypted
let b:GPGEncrypted=0 let b:GPGEncrypted = 0
call s:GPGDebug(1, "this file is not encrypted") call s:GPGDebug(1, "this file is not encrypted")
echohl GPGWarning echohl GPGWarning
echom "File is not encrypted, all GPG functions disabled!" echom "File is not encrypted, all GPG functions disabled!"
@ -292,21 +316,21 @@ function s:GPGDecrypt()
" check if the message is armored " check if the message is armored
if (match(output, "gpg: armor header") >= 0) if (match(output, "gpg: armor header") >= 0)
call s:GPGDebug(1, "this file is armored") call s:GPGDebug(1, "this file is armored")
let b:GPGOptions+=["armor"] let b:GPGOptions += ["armor"]
endif endif
" finally decrypt the buffer content " finally decrypt the buffer content
" since even with the --quiet option passphrase typos will be reported, " since even with the --quiet option passphrase typos will be reported,
" we must redirect stderr (using shell temporarily) " we must redirect stderr (using shell temporarily)
let &shellredir=s:shellredir let &shellredir = s:shellredir
let &shell=s:shell let &shell = s:shell
exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull
let &shellredir=s:shellredirsave let &shellredir = s:shellredirsave
let &shell=s:shellsave let &shell = s:shellsave
if (v:shell_error) " message could not be decrypted if (v:shell_error) " message could not be decrypted
silent u silent u
echohl GPGError echohl GPGError
let blackhole=input("Message could not be decrypted! (Press ENTER)") let blackhole = input("Message could not be decrypted! (Press ENTER)")
echohl None echohl None
bwipeout bwipeout
set nobin set nobin
@ -334,7 +358,7 @@ function s:GPGEncrypt()
call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView)) call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView))
" store encoding and switch to a safe one " store encoding and switch to a safe one
if &fileencoding != &encoding if (&fileencoding != &encoding)
let s:GPGEncoding = &encoding let s:GPGEncoding = &encoding
let &encoding = &fileencoding let &encoding = &fileencoding
call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"") call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"")
@ -356,42 +380,43 @@ function s:GPGEncrypt()
" initialize GPGOptions if not happened before " initialize GPGOptions if not happened before
if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0)
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"]
let b:GPGRecipients = []
else else
let b:GPGOptions+=["encrypt"] let b:GPGOptions += ["encrypt"]
endif endif
if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1)
let b:GPGOptions+=["armor"] let b:GPGOptions += ["armor"]
endif endif
call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions))
endif endif
" built list of options " built list of options
let options="" let options = ""
for option in b:GPGOptions for option in b:GPGOptions
let options=options . " --" . option . " " let options = options . " --" . option . " "
endfor endfor
" 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, unknownrecipients ] = 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 (len(unknownrecipients) > 0)
echohl GPGWarning echohl GPGWarning
echom "Please use GPGEditRecipients to correct!!" echom "Please use GPGEditRecipients to correct!!"
echo echo
echohl None echohl None
" Let user know whats happend and copy known_recipients back to buffer " Let user know whats happend and copy known_recipients back to buffer
let dummy=input("Press ENTER to quit") let dummy = input("Press ENTER to quit")
endif endif
" built list of recipients " built list of recipients
if (len(recipients) > 0) if (len(recipients) > 0)
for gpgid in recipients for gpgid in recipients
let options=options . " -r " . gpgid let options = options . " -r " . gpgid
endfor endfor
else else
if (match(b:GPGOptions, "encrypt") >= 0) if (match(b:GPGOptions, "encrypt") >= 0)
@ -404,18 +429,20 @@ function s:GPGEncrypt()
endif endif
" encrypt the buffer " encrypt the buffer
let &shellredir=s:shellredir let &shellredir = s:shellredir
let &shell=s:shell let &shell = s:shell
silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull
let &shellredir=s:shellredirsave let &shellredir = s:shellredirsave
let &shell=s:shellsave let &shell = s:shellsave
call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull) call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull)
if (v:shell_error) " message could not be encrypted if (v:shell_error) " message could not be encrypted
silent u " delete content of the buffer to be sure no data is written unencrypted
" content will be recovered in GPGEncryptPost()
silent normal! 1GdG
echohl GPGError echohl GPGError
let blackhole=input("Message could not be encrypted! File might be empty! (Press ENTER)") let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)")
echohl None echohl None
bwipeout
return return
endif endif
@ -438,7 +465,7 @@ function s:GPGEncryptPost()
set nobin set nobin
" restore encoding " restore encoding
if s:GPGEncoding != "" if (s:GPGEncoding != "")
let &encoding = s:GPGEncoding let &encoding = s:GPGEncoding
call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"")
endif endif
@ -469,14 +496,14 @@ function s:GPGViewRecipients()
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
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 unknownrecipients
let name="!" . name let name = "!" . name
echo name echo name
endfor endfor
echohl None echohl None
@ -506,8 +533,8 @@ function s:GPGEditRecipients()
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
" save buffer name " save buffer name
let buffername=bufname("%") let buffername = bufname("%")
let editbuffername="GPGRecipients_" . buffername let editbuffername = "GPGRecipients_" . buffername
" check if this buffer exists " check if this buffer exists
if (!bufexists(editbuffername)) if (!bufexists(editbuffername))
@ -541,7 +568,7 @@ function s:GPGEditRecipients()
setlocal nonumber setlocal nonumber
" so we know for which other buffer this edit buffer is " so we know for which other buffer this edit buffer is
let b:GPGCorrespondingTo=buffername let b:GPGCorrespondingTo = buffername
" put some comments to the scratch buffer " put some comments to the scratch buffer
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
@ -554,20 +581,31 @@ function s:GPGEditRecipients()
" get the recipients " get the recipients
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients")) let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients"))
" if there are no known or unknown recipients, use the default ones
if (len(recipients) == 0 && len(unknownrecipients) == 0)
if (type(g:GPGDefaultRecipients) == type([]))
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients)
else
echohl GPGWarning
echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!"
echohl None
endif
endif
" put the recipients in the scratch buffer " put the recipients in the scratch buffer
for name in recipients for name in recipients
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="\\(nonexxistinwordinthisbuffer" let syntaxPattern = "\\(nonexxistinwordinthisbuffer"
for name in unknownrecipients for name in unknownrecipients
let name="!" . name let name = "!" . name
let syntaxPattern=syntaxPattern . "\\|" . name let syntaxPattern = syntaxPattern . "\\|" . name
silent put =name silent put =name
endfor endfor
let syntaxPattern=syntaxPattern . "\\)" let syntaxPattern = syntaxPattern . "\\)"
" define highlight " define highlight
if (has("syntax") && exists("g:syntax_on")) if (has("syntax") && exists("g:syntax_on"))
@ -612,30 +650,30 @@ function s:GPGFinishRecipientsBuffer()
" get the recipients from the scratch buffer " get the recipients from the scratch buffer
let recipients=[] let recipients = []
let lines=getline(1,"$") let lines = getline(1,"$")
for recipient in lines for recipient in lines
" delete all spaces at beginning and end of the recipient " delete all spaces at beginning and end of the recipient
" also delete a '!' at the beginning of the recipient " also delete a '!' at the beginning of the recipient
let recipient=substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
" delete comment lines " delete comment lines
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 (strlen(recipient) > 0)
let gpgid=s:GPGNameToID(recipient) let gpgid = s:GPGNameToID(recipient)
if (strlen(gpgid) > 0) if (strlen(gpgid) > 0)
if (match(recipients, gpgid) < 0) if (match(recipients, gpgid) < 0)
let recipients+=[gpgid] let recipients += [gpgid]
endif endif
else else
if (match(recipients, recipient) < 0) if (match(recipients, recipient) < 0)
let recipients+=[recipient] let recipients += [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
endif endif
end endif
endif endif
endfor endfor
@ -695,8 +733,8 @@ function s:GPGEditOptions()
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
" save buffer name " save buffer name
let buffername=bufname("%") let buffername = bufname("%")
let editbuffername="GPGOptions_" . buffername let editbuffername = "GPGOptions_" . buffername
" check if this buffer exists " check if this buffer exists
if (!bufexists(editbuffername)) if (!bufexists(editbuffername))
@ -729,7 +767,7 @@ function s:GPGEditOptions()
setlocal nonumber setlocal nonumber
" so we know for which other buffer this edit buffer is " so we know for which other buffer this edit buffer is
let b:GPGCorrespondingTo=buffername let b:GPGCorrespondingTo = buffername
" put some comments to the scratch buffer " put some comments to the scratch buffer
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
@ -743,7 +781,7 @@ function s:GPGEditOptions()
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
" put the options in the scratch buffer " put the options in the scratch buffer
let options=getbufvar(b:GPGCorrespondingTo, "GPGOptions") let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions")
for option in options for option in options
silent put =option silent put =option
@ -783,24 +821,24 @@ function s:GPGFinishOptionsBuffer()
endif endif
" clear options and unknownOptions " clear options and unknownOptions
let options=[] let options = []
let unknownOptions=[] let unknownOptions = []
" delete the autocommand " delete the autocommand
autocmd! * <buffer> autocmd! * <buffer>
" get the options from the scratch buffer " get the options from the scratch buffer
let lines=getline(1, "$") let lines = getline(1, "$")
for option in lines for option in lines
" delete all spaces at beginning and end of the option " delete all spaces at beginning and end of the option
" also delete a '!' at the beginning of the option " also delete a '!' at the beginning of the option
let option=substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") let option = substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
" delete comment lines " delete comment lines
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 (strlen(option) > 0 && match(options, option) < 0)
let options+=[option] let options += [option]
endif endif
endfor endfor
@ -818,19 +856,19 @@ endfunction
" check if recipients are known " check if recipients are known
" Returns: two lists recipients and unknownrecipients " Returns: two lists recipients and unknownrecipients
function s:GPGCheckRecipients(tocheck) function s:GPGCheckRecipients(tocheck)
let recipients=[] let recipients = []
let unknownrecipients=[] 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 (strlen(gpgid) > 0)
if (match(recipients, gpgid) < 0) if (match(recipients, gpgid) < 0)
let recipients+=[gpgid] let recipients += [gpgid]
endif endif
else else
if (match(unknownrecipients, recipient) < 0) if (match(unknownrecipients, recipient) < 0)
let unknownrecipients+=[recipient] let unknownrecipients += [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
@ -851,60 +889,60 @@ endfunction
" Returns: ID for the given name " Returns: ID for the given name
function s:GPGNameToID(name) function s:GPGNameToID(name)
" ask gpg for the id for a name " ask gpg for the id for a name
let &shellredir=s:shellredir let &shellredir = s:shellredir
let &shell=s:shell let &shell = s:shell
let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"") let output = system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"")
let &shellredir=s:shellredirsave let &shellredir = s:shellredirsave
let &shell=s:shellsave let &shell = s:shellsave
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
" so convert it, if necessary " so convert it, if necessary
if &encoding != "utf-8" if (&encoding != "utf-8")
let output=iconv(output, "utf-8", &encoding) let output = iconv(output, "utf-8", &encoding)
endif endif
let lines=split(output, "\n") let lines = split(output, "\n")
" parse the output of gpg " parse the output of gpg
let pubseen=0 let pubseen = 0
let uidseen=0 let uidseen = 0
let counter=0 let counter = 0
let gpgids=[] let gpgids = []
let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n"
for line in lines for line in lines
let fields=split(line, ":") let fields = split(line, ":")
" search for the next uid " search for the next uid
if (pubseen == 1) if (pubseen == 1)
if (fields[0] == "uid") if (fields[0] == "uid")
if (uidseen == 0) if (uidseen == 0)
let choices=choices . counter . ": " . fields[9] . "\n" let choices = choices . counter . ": " . fields[9] . "\n"
let counter=counter+1 let counter = counter+1
let uidseen=1 let uidseen = 1
else else
let choices=choices . " " . fields[9] . "\n" let choices = choices . " " . fields[9] . "\n"
endif endif
else else
let uidseen=0 let uidseen = 0
let pubseen=0 let pubseen = 0
endif endif
endif endif
" search for the next pub " search for the next pub
if (pubseen == 0) if (pubseen == 0)
if (fields[0] == "pub") if (fields[0] == "pub")
let gpgids+=[fields[4]] let gpgids += [fields[4]]
let pubseen=1 let pubseen = 1
endif endif
endif endif
endfor endfor
" counter > 1 means we have more than one results " counter > 1 means we have more than one results
let answer=0 let answer = 0
if (counter > 1) if (counter > 1)
let choices=choices . "Enter number: " let choices = choices . "Enter number: "
let answer=input(choices, "0") let answer = input(choices, "0")
while (answer == "") while (answer == "")
let answer=input("Enter number: ", "0") let answer = input("Enter number: ", "0")
endwhile endwhile
endif endif
@ -919,32 +957,32 @@ function s:GPGIDToName(identity)
" TODO is the encryption subkey really unique? " TODO is the encryption subkey really unique?
" ask gpg for the id for a name " ask gpg for the id for a name
let &shellredir=s:shellredir let &shellredir = s:shellredir
let &shell=s:shell let &shell = s:shell
let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity ) let output = system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity )
let &shellredir=s:shellredirsave let &shellredir = s:shellredirsave
let &shell=s:shellsave let &shell = s:shellsave
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
" so convert it, if necessary " so convert it, if necessary
if &encoding != "utf-8" if (&encoding != "utf-8")
let output=iconv(output, "utf-8", &encoding) let output = iconv(output, "utf-8", &encoding)
endif endif
let lines=split(output, "\n") let lines = split(output, "\n")
" parse the output of gpg " parse the output of gpg
let pubseen=0 let pubseen = 0
let uid="" let uid = ""
for line in lines for line in lines
let fields=split(line, ":") let fields = split(line, ":")
if (pubseen == 0) " search for the next pub if (pubseen == 0) " search for the next pub
if (fields[0] == "pub") if (fields[0] == "pub")
let pubseen=1 let pubseen = 1
endif endif
else " search for the next uid else " search for the next uid
if (fields[0] == "uid") if (fields[0] == "uid")
let pubseen=0 let pubseen = 0
let uid=fields[9] let uid = fields[9]
break break
endif endif
endif endif
@ -968,7 +1006,7 @@ command! GPGEditRecipients call s:GPGEditRecipients()
command! GPGViewOptions call s:GPGViewOptions() command! GPGViewOptions call s:GPGViewOptions()
command! GPGEditOptions call s:GPGEditOptions() command! GPGEditOptions call s:GPGEditOptions()
" Section: Menu {{{1 " Section: Menu {{{1
if has("menu") if (has("menu"))
amenu <silent> Plugin.GnuPG.View\ Recipients :GPGViewRecipients<CR> amenu <silent> Plugin.GnuPG.View\ Recipients :GPGViewRecipients<CR>
amenu <silent> Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients<CR> amenu <silent> Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients<CR>
amenu <silent> Plugin.GnuPG.View\ Options :GPGViewOptions<CR> amenu <silent> Plugin.GnuPG.View\ Options :GPGViewOptions<CR>