2002-04-24 04:59:12 -04:00
|
|
|
" Name: gnupg.vim
|
2007-12-11 04:43:38 -05:00
|
|
|
" Version: $Id$
|
|
|
|
" Author: Markus Braun <markus.braun@krawel.de>
|
|
|
|
" Summary: Vim plugin for transparent editing of gpg encrypted files.
|
|
|
|
" Licence: This program is free software; you can redistribute it and/or
|
|
|
|
" modify it under the terms of the GNU General Public License.
|
|
|
|
" See http://www.gnu.org/copyleft/gpl.txt
|
2011-07-05 02:55:50 -04:00
|
|
|
" Section: Documentation {{{1
|
2002-04-24 04:59:12 -04:00
|
|
|
" Description:
|
|
|
|
"
|
|
|
|
" This script implements transparent editing of gpg encrypted files. The
|
2006-12-12 17:46:03 -05:00
|
|
|
" filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such
|
|
|
|
" a file the content is decrypted, when opening a new file the script will
|
|
|
|
" ask for the recipients of the encrypted file. The file content will be
|
|
|
|
" encrypted to all recipients before it is written. The script turns off
|
|
|
|
" viminfo and swapfile to increase security.
|
2002-04-24 04:59:12 -04:00
|
|
|
"
|
|
|
|
" Installation:
|
|
|
|
"
|
|
|
|
" Copy the gnupg.vim file to the $HOME/.vim/plugin directory.
|
|
|
|
" Refer to ':help add-plugin', ':help add-global-plugin' and ':help
|
|
|
|
" runtimepath' for more details about Vim plugins.
|
|
|
|
"
|
|
|
|
" Commands:
|
|
|
|
"
|
|
|
|
" :GPGEditRecipients
|
|
|
|
" Opens a scratch buffer to change the list of recipients. Recipients that
|
2006-12-14 05:03:19 -05:00
|
|
|
" are unknown (not in your public key) are highlighted and have
|
|
|
|
" a prepended "!". Closing the buffer makes the changes permanent.
|
2002-04-24 04:59:12 -04:00
|
|
|
"
|
|
|
|
" :GPGViewRecipients
|
|
|
|
" Prints the list of recipients.
|
|
|
|
"
|
|
|
|
" :GPGEditOptions
|
|
|
|
" Opens a scratch buffer to change the options for encryption (symmetric,
|
2006-12-14 05:03:19 -05:00
|
|
|
" asymmetric, signing). Closing the buffer makes the changes permanent.
|
2002-04-24 04:59:12 -04:00
|
|
|
" WARNING: There is no check of the entered options, so you need to know
|
|
|
|
" what you are doing.
|
|
|
|
"
|
2011-07-05 02:46:55 -04:00
|
|
|
" :GPGViewOptions
|
2002-04-24 04:59:12 -04:00
|
|
|
" Prints the list of options.
|
|
|
|
"
|
2006-11-16 10:29:55 -05:00
|
|
|
" Variables:
|
2006-12-08 11:02:33 -05:00
|
|
|
"
|
2006-11-16 11:12:57 -05:00
|
|
|
" g:GPGUseAgent
|
2006-11-21 03:54:56 -05:00
|
|
|
" If set to 0 a possible available gpg-agent won't be used. Defaults to 1.
|
2006-11-16 10:29:55 -05:00
|
|
|
"
|
2006-12-17 16:20:33 -05:00
|
|
|
" g:GPGPreferSymmetric
|
|
|
|
" If set to 1 symmetric encryption is preferred for new files. Defaults to 0.
|
|
|
|
"
|
|
|
|
" g:GPGPreferArmor
|
|
|
|
" If set to 1 armored data is preferred for new files. Defaults to 0.
|
|
|
|
"
|
2002-04-24 04:59:12 -04:00
|
|
|
" Credits:
|
|
|
|
" Mathieu Clabaut for inspirations through his vimspell.vim script.
|
2006-12-12 17:46:03 -05:00
|
|
|
" Richard Bronosky for patch to enable ".pgp" suffix.
|
2011-07-05 02:46:55 -04:00
|
|
|
" Erik Remmelzwaal for patch to enable windows support and patient beta
|
|
|
|
" testing.
|
2007-12-11 04:43:38 -05:00
|
|
|
" Lars Becker for patch to make gpg2 working.
|
2006-12-12 17:46:03 -05:00
|
|
|
"
|
2002-04-24 04:59:12 -04:00
|
|
|
" Section: Plugin header {{{1
|
2007-04-05 06:50:38 -04:00
|
|
|
if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)"))
|
2002-04-24 04:59:12 -04:00
|
|
|
finish
|
|
|
|
endi
|
2007-04-05 06:50:38 -04:00
|
|
|
let g:loaded_gnupg = "$Revision$"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" Section: Autocmd setup {{{1
|
|
|
|
augroup GnuPG
|
2011-07-05 02:56:18 -04:00
|
|
|
autocmd!
|
|
|
|
|
|
|
|
" initialize the internal variables
|
2006-12-08 11:02:33 -05:00
|
|
|
autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit()
|
2011-07-05 02:56:18 -04:00
|
|
|
" force the user to edit the recipient list if he opens a new file and public
|
2006-12-17 16:20:33 -05:00
|
|
|
" keys are preferred
|
|
|
|
autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endi
|
2011-07-05 02:56:18 -04:00
|
|
|
" do the decryption
|
2006-12-08 11:02:33 -05:00
|
|
|
autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt()
|
2011-07-05 02:56:18 -04:00
|
|
|
|
|
|
|
" convert all text to encrypted text before writing
|
2006-12-08 11:02:33 -05:00
|
|
|
autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt()
|
2011-07-05 02:56:18 -04:00
|
|
|
" undo the encryption so we are back in the normal text, directly
|
2002-04-24 04:59:12 -04:00
|
|
|
" after the file has been written.
|
2011-07-05 02:56:18 -04:00
|
|
|
autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost()
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
augroup END
|
2011-07-05 02:56:18 -04:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" Section: Highlight setup {{{1
|
2011-07-05 02:47:10 -04:00
|
|
|
highlight default link GPGWarning WarningMsg
|
|
|
|
highlight default link GPGError ErrorMsg
|
|
|
|
highlight default link GPGHighlightUnknownRecipient ErrorMsg
|
2011-07-05 02:56:18 -04:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" Section: Functions {{{1
|
|
|
|
" Function: s:GPGInit() {{{2
|
|
|
|
"
|
|
|
|
" initialize the plugin
|
|
|
|
"
|
|
|
|
fun s:GPGInit()
|
2011-07-05 02:56:18 -04:00
|
|
|
" first make sure nothing is written to ~/.viminfo while editing
|
|
|
|
" an encrypted file.
|
|
|
|
set viminfo=
|
|
|
|
|
|
|
|
" we don't want a swap file, as it writes unencrypted data to disk
|
|
|
|
set noswapfile
|
|
|
|
|
2006-11-16 10:29:55 -05:00
|
|
|
" check if gpg-agent is allowed
|
2006-11-16 11:12:57 -05:00
|
|
|
if (!exists("g:GPGUseAgent"))
|
2006-11-21 03:54:56 -05:00
|
|
|
let g:GPGUseAgent = 1
|
2006-11-16 10:29:55 -05:00
|
|
|
endif
|
|
|
|
|
2006-12-17 16:20:33 -05:00
|
|
|
" check if symmetric encryption is preferred
|
|
|
|
if (!exists("g:GPGPreferSymmetric"))
|
|
|
|
let g:GPGPreferSymmetric = 0
|
|
|
|
endif
|
|
|
|
|
|
|
|
" check if armored files are preferred
|
|
|
|
if (!exists("g:GPGPreferArmor"))
|
|
|
|
let g:GPGPreferArmor = 0
|
|
|
|
endif
|
|
|
|
|
2011-07-05 02:51:02 -04:00
|
|
|
" check if debugging is turned on
|
|
|
|
if (!exists("g:GPGDebugLevel"))
|
|
|
|
let g:GPGDebugLevel = 0
|
|
|
|
endif
|
2008-01-23 04:00:51 -05:00
|
|
|
|
|
|
|
" print version
|
|
|
|
call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg)
|
2011-07-05 02:51:02 -04:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" determine if gnupg can use the gpg-agent
|
2006-11-16 11:12:57 -05:00
|
|
|
if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1)
|
|
|
|
if (!exists("$GPG_TTY"))
|
|
|
|
echohl GPGError
|
|
|
|
echo "The GPG_TTY is not set!"
|
|
|
|
echo "gpg-agent might not work."
|
|
|
|
echohl None
|
|
|
|
endif
|
2006-12-13 09:58:58 -05:00
|
|
|
let s:GPGCommand="gpg --use-agent"
|
2002-04-24 04:59:12 -04:00
|
|
|
else
|
2006-12-13 09:58:58 -05:00
|
|
|
let s:GPGCommand="gpg --no-use-agent"
|
2002-04-24 04:59:12 -04:00
|
|
|
endif
|
|
|
|
|
2007-03-01 04:58:04 -05:00
|
|
|
" don't use tty in gvim
|
|
|
|
" FIXME find a better way to avoid an error.
|
|
|
|
" with this solution only --use-agent will work
|
|
|
|
if has("gui_running")
|
|
|
|
let s:GPGCommand=s:GPGCommand . " --no-tty"
|
|
|
|
endif
|
|
|
|
|
2006-12-08 05:27:58 -05:00
|
|
|
" setup shell environment for unix and windows
|
|
|
|
let s:shellredirsave=&shellredir
|
|
|
|
let s:shellsave=&shell
|
2011-07-05 02:44:14 -04:00
|
|
|
if (match(&shell,"\\(cmd\\|command\\).exe") >= 0)
|
2006-12-08 05:27:58 -05:00
|
|
|
" windows specific settings
|
|
|
|
let s:shellredir = '>%s'
|
|
|
|
let s:shell = &shell
|
2011-07-05 02:44:44 -04:00
|
|
|
let s:stderrredirnull = '2>nul'
|
2006-12-08 05:27:58 -05:00
|
|
|
else
|
|
|
|
" unix specific settings
|
|
|
|
let s:shellredir = &shellredir
|
|
|
|
let s:shell = 'sh'
|
2011-07-05 02:44:44 -04:00
|
|
|
let s:stderrredirnull ='2>/dev/null'
|
2006-12-28 08:05:56 -05:00
|
|
|
let s:GPGCommand="LANG=C LC_ALL=C " . s:GPGCommand
|
2006-12-08 05:27:58 -05:00
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" find the supported algorithms
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shellredir=s:shellredir
|
|
|
|
let &shell=s:shell
|
2006-11-16 10:29:55 -05:00
|
|
|
let output=system(s:GPGCommand . " --version")
|
2007-01-29 08:38:16 -05:00
|
|
|
let &shellredir=s:shellredirsave
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shell=s:shellsave
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
let s:GPGPubkey=substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "")
|
|
|
|
let s:GPGCipher=substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "")
|
|
|
|
let s:GPGHash=substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "")
|
|
|
|
let s:GPGCompress=substitute(output, ".*Compress: \\(.\\{-}\\)\n.*", "\\1", "")
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGDecrypt() {{{2
|
|
|
|
"
|
|
|
|
" decrypt the buffer and find all recipients of the encrypted file
|
|
|
|
"
|
|
|
|
fun s:GPGDecrypt()
|
2011-07-05 02:56:18 -04:00
|
|
|
" switch to binary mode to read the encrypted file
|
|
|
|
set bin
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" get the filename of the current buffer
|
2006-12-14 02:34:09 -05:00
|
|
|
let filename=escape(expand("%:p"), '\"')
|
2002-04-24 04:59:12 -04:00
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
" clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions
|
|
|
|
let b:GPGEncrypted=0
|
2002-04-24 04:59:12 -04:00
|
|
|
let b:GPGRecipients=""
|
|
|
|
let b:GPGUnknownRecipients=""
|
|
|
|
let b:GPGOptions=""
|
|
|
|
|
|
|
|
" find the recipients of the file
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shellredir=s:shellredir
|
|
|
|
let &shell=s:shell
|
2011-07-05 02:51:27 -04:00
|
|
|
let output=system(s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\"")
|
2007-01-29 08:38:16 -05:00
|
|
|
let &shellredir=s:shellredirsave
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shell=s:shellsave
|
2011-07-05 02:51:02 -04:00
|
|
|
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 . " <<<<<")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" check if the file is symmetric/asymmetric encrypted
|
2007-12-11 04:43:38 -05:00
|
|
|
if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
" file is symmetric encrypted
|
2006-12-13 10:28:31 -05:00
|
|
|
let b:GPGEncrypted=1
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "this file is symmetric encrypted")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
let b:GPGOptions=b:GPGOptions . "symmetric:"
|
|
|
|
|
|
|
|
let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "")
|
|
|
|
if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0)
|
|
|
|
let b:GPGOptions=b:GPGOptions . "cipher-algo " . cipher . ":"
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "cipher-algo is " . cipher)
|
2002-04-24 04:59:12 -04:00
|
|
|
else
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "The cipher " . cipher . " is not known by the local gpg command. Using default!"
|
|
|
|
echo
|
|
|
|
echohl None
|
|
|
|
endi
|
2007-12-11 04:43:38 -05:00
|
|
|
elseif (match(output, "gpg: public key is [[:xdigit:]]\\{8}") >= 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
" file is asymmetric encrypted
|
2006-12-13 10:28:31 -05:00
|
|
|
let b:GPGEncrypted=1
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "this file is asymmetric encrypted")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
let b:GPGOptions=b:GPGOptions . "encrypt:"
|
|
|
|
|
2011-07-05 02:51:02 -04:00
|
|
|
let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}")
|
2002-04-24 04:59:12 -04:00
|
|
|
while (start >= 0)
|
2011-07-05 02:51:02 -04:00
|
|
|
let start=start + strlen("gpg: public key is ")
|
2002-04-24 04:59:12 -04:00
|
|
|
let recipient=strpart(output, start, 8)
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "recipient is " . recipient)
|
2002-04-24 04:59:12 -04:00
|
|
|
let name=s:GPGNameToID(recipient)
|
|
|
|
if (strlen(name) > 0)
|
|
|
|
let b:GPGRecipients=b:GPGRecipients . name . ":"
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "name of recipient is " . name)
|
2002-04-24 04:59:12 -04:00
|
|
|
else
|
|
|
|
let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":"
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "The recipient " . recipient . " is not in your public keyring!"
|
|
|
|
echohl None
|
|
|
|
end
|
2011-07-05 02:51:02 -04:00
|
|
|
let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start)
|
2002-04-24 04:59:12 -04:00
|
|
|
endw
|
2007-12-11 04:43:38 -05:00
|
|
|
else
|
2006-12-13 10:28:31 -05:00
|
|
|
" file is not encrypted
|
|
|
|
let b:GPGEncrypted=0
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "this file is not encrypted")
|
2006-12-13 10:28:31 -05:00
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
2011-07-05 02:56:18 -04:00
|
|
|
set nobin
|
2006-12-13 10:28:31 -05:00
|
|
|
return
|
2002-04-24 04:59:12 -04:00
|
|
|
endi
|
|
|
|
|
|
|
|
" check if the message is armored
|
2011-07-05 02:51:02 -04:00
|
|
|
if (match(output, "gpg: armor header") >= 0)
|
|
|
|
call s:GPGDebug(1, "this file is armored")
|
2002-04-24 04:59:12 -04:00
|
|
|
let b:GPGOptions=b:GPGOptions . "armor:"
|
|
|
|
endi
|
|
|
|
|
|
|
|
" finally decrypt the buffer content
|
|
|
|
" since even with the --quiet option passphrase typos will be reported,
|
2006-12-08 05:27:58 -05:00
|
|
|
" we must redirect stderr (using shell temporarily)
|
|
|
|
let &shellredir=s:shellredir
|
|
|
|
let &shell=s:shell
|
2011-07-05 02:44:44 -04:00
|
|
|
exec "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull
|
2007-01-29 08:38:16 -05:00
|
|
|
let &shellredir=s:shellredirsave
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shell=s:shellsave
|
2002-04-24 04:59:12 -04:00
|
|
|
if (v:shell_error) " message could not be decrypted
|
|
|
|
silent u
|
|
|
|
echohl GPGError
|
|
|
|
let asd=input("Message could not be decrypted! (Press ENTER)")
|
|
|
|
echohl None
|
|
|
|
bwipeout
|
2011-07-05 02:56:18 -04:00
|
|
|
set nobin
|
2002-04-24 04:59:12 -04:00
|
|
|
return
|
|
|
|
endi
|
2011-07-05 02:56:18 -04:00
|
|
|
|
|
|
|
" turn off binary mode
|
|
|
|
set nobin
|
|
|
|
|
|
|
|
" call the autocommand for the file minus .gpg$
|
|
|
|
execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'")
|
|
|
|
call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'"))
|
|
|
|
|
|
|
|
" refresh screen
|
|
|
|
redraw!
|
2002-04-24 04:59:12 -04:00
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGEncrypt() {{{2
|
|
|
|
"
|
|
|
|
" encrypts the buffer to all previous recipients
|
|
|
|
"
|
|
|
|
fun s:GPGEncrypt()
|
2011-07-05 02:55:50 -04:00
|
|
|
" save cursor position
|
|
|
|
let s:GPGCursorPosition = getpos(".")
|
|
|
|
call s:GPGDebug(2, "saved cursor position " . string(s:GPGCursorPosition))
|
|
|
|
|
2008-01-23 03:58:02 -05:00
|
|
|
" store encoding and switch to a safe one
|
|
|
|
if &fileencoding != &encoding
|
|
|
|
let s:GPGEncoding = &encoding
|
|
|
|
let &encoding = &fileencoding
|
|
|
|
call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"")
|
|
|
|
else
|
|
|
|
let s:GPGEncoding = ""
|
|
|
|
call s:GPGDebug(2, "encoding and fileencoding are the same (\"" . &encoding . "\"), not switching")
|
|
|
|
endi
|
|
|
|
|
2011-07-05 02:55:50 -04:00
|
|
|
" switch buffer to binary mode
|
|
|
|
set bin
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
let options=""
|
|
|
|
let recipients=""
|
|
|
|
let field=0
|
|
|
|
|
|
|
|
" built list of options
|
|
|
|
if (!exists("b:GPGOptions") || strlen(b:GPGOptions) == 0)
|
2006-12-17 16:20:33 -05:00
|
|
|
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
|
|
|
|
let b:GPGOptions="symmetric:"
|
|
|
|
else
|
|
|
|
let b:GPGOptions="encrypt:"
|
|
|
|
endi
|
|
|
|
if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1)
|
|
|
|
let b:GPGOptions=b:GPGOptions . "armor:"
|
|
|
|
endi
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "no options set, so using default options: " . b:GPGOptions)
|
2002-04-24 04:59:12 -04:00
|
|
|
endi
|
|
|
|
let field=0
|
|
|
|
let option=s:GetField(b:GPGOptions, ":", field)
|
|
|
|
while (strlen(option))
|
|
|
|
let options=options . " --" . option . " "
|
|
|
|
let field=field+1
|
|
|
|
let option=s:GetField(b:GPGOptions, ":", field)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" check if there are unknown recipients and warn
|
|
|
|
if (exists("b:GPGUnknownRecipients") && strlen(b:GPGUnknownRecipients) > 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "There are unknown recipients!!"
|
|
|
|
echo "Please use GPGEditRecipients to correct!!"
|
|
|
|
echo
|
|
|
|
echohl None
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "unknown recipients are: " . b:GPGUnknownRecipients)
|
2002-04-24 04:59:12 -04:00
|
|
|
endi
|
|
|
|
|
|
|
|
" built list of recipients
|
|
|
|
if (exists("b:GPGRecipients") && strlen(b:GPGRecipients) > 0)
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "recipients are: " . b:GPGRecipients)
|
2002-04-24 04:59:12 -04:00
|
|
|
let field=0
|
|
|
|
let gpgid=s:GetField(b:GPGRecipients, ":", field)
|
|
|
|
while (strlen(gpgid))
|
|
|
|
let recipients=recipients . " -r " . gpgid
|
|
|
|
let field=field+1
|
|
|
|
let gpgid=s:GetField(b:GPGRecipients, ":", field)
|
|
|
|
endw
|
|
|
|
else
|
2006-12-08 11:02:33 -05:00
|
|
|
if (match(b:GPGOptions, "encrypt:") >= 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
echohl GPGError
|
|
|
|
echo "There are no recipients!!"
|
|
|
|
echo "Please use GPGEditRecipients to correct!!"
|
|
|
|
echo
|
|
|
|
echohl None
|
|
|
|
endi
|
|
|
|
endi
|
|
|
|
|
|
|
|
" encrypt the buffer
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shellredir=s:shellredir
|
|
|
|
let &shell=s:shell
|
2011-07-05 02:44:44 -04:00
|
|
|
silent exec "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull
|
2007-01-29 08:38:16 -05:00
|
|
|
let &shellredir=s:shellredirsave
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shell=s:shellsave
|
2011-07-05 02:51:02 -04:00
|
|
|
call s:GPGDebug(1, "called gpg command is: " . "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . recipients . " " . s:stderrredirnull)
|
2002-04-24 04:59:12 -04:00
|
|
|
if (v:shell_error) " message could not be encrypted
|
|
|
|
silent u
|
|
|
|
echohl GPGError
|
|
|
|
let asd=input("Message could not be encrypted! File might be empty! (Press ENTER)")
|
|
|
|
echohl None
|
|
|
|
bwipeout
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2011-07-05 02:55:50 -04:00
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGEncryptPost() {{{2
|
|
|
|
"
|
|
|
|
" undo changes don by encrypt, after writing
|
|
|
|
"
|
|
|
|
fun s:GPGEncryptPost()
|
|
|
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
|
|
|
" undo encryption of buffer content
|
|
|
|
silent u
|
|
|
|
|
|
|
|
" switch back from binary mode
|
|
|
|
set nobin
|
|
|
|
|
2008-01-23 03:58:02 -05:00
|
|
|
" restore encoding
|
|
|
|
if s:GPGEncoding != ""
|
|
|
|
let &encoding = s:GPGEncoding
|
|
|
|
call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"")
|
|
|
|
endi
|
|
|
|
|
2011-07-05 02:55:50 -04:00
|
|
|
" restore cursor position
|
|
|
|
call setpos('.', s:GPGCursorPosition)
|
|
|
|
call s:GPGDebug(2, "restored cursor position " . string(s:GPGCursorPosition))
|
|
|
|
|
|
|
|
" refresh screen
|
2011-07-05 02:51:02 -04:00
|
|
|
redraw!
|
2002-04-24 04:59:12 -04:00
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGViewRecipients() {{{2
|
|
|
|
"
|
|
|
|
" echo the recipients
|
|
|
|
"
|
|
|
|
fun s:GPGViewRecipients()
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
if (exists("b:GPGRecipients"))
|
|
|
|
echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
|
|
|
|
" echo the recipients
|
|
|
|
let field=0
|
|
|
|
let name=s:GetField(b:GPGRecipients, ":", field)
|
|
|
|
while (strlen(name) > 0)
|
|
|
|
let name=s:GPGIDToName(name)
|
|
|
|
echo name
|
|
|
|
|
|
|
|
let field=field+1
|
|
|
|
let name=s:GetField(b:GPGRecipients, ":", field)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" put the unknown recipients in the scratch buffer
|
|
|
|
let field=0
|
|
|
|
echohl GPGWarning
|
|
|
|
let name=s:GetField(b:GPGUnknownRecipients, ":", field)
|
|
|
|
while (strlen(name) > 0)
|
|
|
|
let name="!" . name
|
|
|
|
echo name
|
|
|
|
|
|
|
|
let field=field+1
|
|
|
|
let name=s:GetField(b:GPGUnknownRecipients, ":", field)
|
|
|
|
endw
|
|
|
|
echohl None
|
|
|
|
|
|
|
|
" check if there is any known recipient
|
|
|
|
if (strlen(s:GetField(b:GPGRecipients, ":", 0)) == 0)
|
|
|
|
echohl GPGError
|
|
|
|
echo 'There are no known recipients!'
|
|
|
|
echohl None
|
|
|
|
endi
|
|
|
|
endi
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGEditRecipients() {{{2
|
|
|
|
"
|
|
|
|
" create a scratch buffer with all recipients to add/remove recipients
|
|
|
|
"
|
|
|
|
fun s:GPGEditRecipients()
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" only do this if it isn't already a GPGRecipients_* buffer
|
2006-12-08 11:02:33 -05:00
|
|
|
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" save buffer name
|
2006-12-14 05:03:19 -05:00
|
|
|
let buffername=bufname("%")
|
2002-04-24 04:59:12 -04:00
|
|
|
let editbuffername="GPGRecipients_" . buffername
|
|
|
|
|
|
|
|
" check if this buffer exists
|
2006-12-14 05:03:19 -05:00
|
|
|
if (!bufexists(editbuffername))
|
|
|
|
" create scratch buffer
|
|
|
|
exe 'silent! split ' . escape(editbuffername, ' *?\"'."'")
|
|
|
|
|
|
|
|
" add a autocommand to regenerate the recipients after a write
|
|
|
|
autocmd BufHidden,BufUnload <buffer> call s:GPGFinishRecipientsBuffer()
|
|
|
|
else
|
|
|
|
if (bufwinnr(editbuffername) >= 0)
|
|
|
|
" switch to scratch buffer window
|
|
|
|
exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
|
|
|
|
else
|
|
|
|
" split scratch buffer window
|
|
|
|
exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'")
|
2006-12-14 05:51:22 -05:00
|
|
|
|
|
|
|
" add a autocommand to regenerate the recipients after a write
|
|
|
|
autocmd BufHidden,BufUnload <buffer> call s:GPGFinishRecipientsBuffer()
|
2006-12-14 05:03:19 -05:00
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" empty the buffer
|
|
|
|
silent normal! 1GdG
|
|
|
|
endi
|
|
|
|
|
|
|
|
" Mark the buffer as a scratch buffer
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal noswapfile
|
|
|
|
setlocal nowrap
|
|
|
|
setlocal nobuflisted
|
|
|
|
setlocal nonumber
|
|
|
|
|
|
|
|
" so we know for which other buffer this edit buffer is
|
|
|
|
let b:corresponding_to=buffername
|
|
|
|
|
|
|
|
" put some comments to the scratch buffer
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
silent put ='GPG: Please edit the list of recipients, one recipient per line'
|
|
|
|
silent put ='GPG: Unknown recipients have a prepended \"!\"'
|
|
|
|
silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically'
|
2006-12-14 05:03:19 -05:00
|
|
|
silent put ='GPG: Closing this buffer commits changes'
|
2002-04-24 04:59:12 -04:00
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
|
|
|
|
" put the recipients in the scratch buffer
|
|
|
|
let recipients=getbufvar(b:corresponding_to, "GPGRecipients")
|
|
|
|
let field=0
|
|
|
|
|
|
|
|
let name=s:GetField(recipients, ":", field)
|
|
|
|
while (strlen(name) > 0)
|
|
|
|
let name=s:GPGIDToName(name)
|
|
|
|
silent put =name
|
|
|
|
|
|
|
|
let field=field+1
|
|
|
|
let name=s:GetField(recipients, ":", field)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" put the unknown recipients in the scratch buffer
|
|
|
|
let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients")
|
|
|
|
let field=0
|
|
|
|
let syntaxPattern="\\(nonexistingwordinthisbuffer"
|
|
|
|
|
|
|
|
let name=s:GetField(unknownRecipients, ":", field)
|
|
|
|
while (strlen(name) > 0)
|
|
|
|
let name="!" . name
|
|
|
|
let syntaxPattern=syntaxPattern . "\\|" . name
|
|
|
|
silent put =name
|
|
|
|
|
|
|
|
let field=field+1
|
|
|
|
let name=s:GetField(unknownRecipients, ":", field)
|
|
|
|
endw
|
|
|
|
|
|
|
|
let syntaxPattern=syntaxPattern . "\\)"
|
|
|
|
|
|
|
|
" define highlight
|
|
|
|
if (has("syntax") && exists("g:syntax_on"))
|
|
|
|
exec('syntax match GPGUnknownRecipient "' . syntaxPattern . '"')
|
|
|
|
highlight clear GPGUnknownRecipient
|
|
|
|
highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient
|
|
|
|
|
|
|
|
syntax match GPGComment "^GPG:.*$"
|
|
|
|
highlight clear GPGComment
|
|
|
|
highlight link GPGComment Comment
|
|
|
|
endi
|
|
|
|
|
|
|
|
" delete the empty first line
|
|
|
|
silent normal! 1Gdd
|
|
|
|
|
|
|
|
" jump to the first recipient
|
2006-12-13 09:58:58 -05:00
|
|
|
silent normal! G
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
endi
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGFinishRecipientsBuffer() {{{2
|
|
|
|
"
|
|
|
|
" create a new recipient list from RecipientsBuffer
|
|
|
|
fun s:GPGFinishRecipientsBuffer()
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2006-12-14 05:51:22 -05:00
|
|
|
" go to buffer before doing work
|
|
|
|
if (bufnr("%") != expand("<abuf>"))
|
|
|
|
" switch to scratch buffer window
|
|
|
|
exe 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" clear GPGRecipients and GPGUnknownRecipients
|
|
|
|
let GPGRecipients=""
|
|
|
|
let GPGUnknownRecipients=""
|
|
|
|
|
|
|
|
" delete the autocommand
|
2006-12-14 05:03:19 -05:00
|
|
|
autocmd! * <buffer>
|
2006-12-14 05:51:22 -05:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
let currentline=1
|
|
|
|
let recipient=getline(currentline)
|
|
|
|
|
|
|
|
" get the recipients from the scratch buffer
|
|
|
|
while (currentline <= line("$"))
|
|
|
|
" delete all spaces at beginning and end of the line
|
|
|
|
" also delete a '!' at the beginning of the line
|
|
|
|
let recipient=substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
|
|
|
|
" delete comment lines
|
|
|
|
let recipient=substitute(recipient, "^GPG:.*$", "", "")
|
|
|
|
|
|
|
|
" only do this if the line is not empty
|
|
|
|
if (strlen(recipient) > 0)
|
|
|
|
let gpgid=s:GPGNameToID(recipient)
|
|
|
|
if (strlen(gpgid) > 0)
|
|
|
|
let GPGRecipients=GPGRecipients . gpgid . ":"
|
|
|
|
else
|
|
|
|
let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":"
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "The recipient " . recipient . " is not in your public keyring!"
|
|
|
|
echohl None
|
|
|
|
end
|
|
|
|
endi
|
|
|
|
|
|
|
|
let currentline=currentline+1
|
|
|
|
let recipient=getline(currentline)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" write back the new recipient list to the corresponding buffer and mark it
|
2006-12-14 02:34:09 -05:00
|
|
|
" as modified. Buffer is now for sure a encrypted buffer.
|
2002-04-24 04:59:12 -04:00
|
|
|
call setbufvar(b:corresponding_to, "GPGRecipients", GPGRecipients)
|
|
|
|
call setbufvar(b:corresponding_to, "GPGUnknownRecipients", GPGUnknownRecipients)
|
|
|
|
call setbufvar(b:corresponding_to, "&mod", 1)
|
2006-12-14 02:34:09 -05:00
|
|
|
call setbufvar(b:corresponding_to, "GPGEncrypted", 1)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" check if there is any known recipient
|
|
|
|
if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0)
|
|
|
|
echohl GPGError
|
|
|
|
echo 'There are no known recipients!'
|
|
|
|
echohl None
|
|
|
|
endi
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGViewOptions() {{{2
|
|
|
|
"
|
|
|
|
" echo the recipients
|
|
|
|
"
|
|
|
|
fun s:GPGViewOptions()
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
if (exists("b:GPGOptions"))
|
|
|
|
echo 'This file has following options:'
|
|
|
|
" echo the options
|
|
|
|
let field=0
|
|
|
|
let option=s:GetField(b:GPGOptions, ":", field)
|
|
|
|
while (strlen(option) > 0)
|
|
|
|
echo option
|
|
|
|
|
|
|
|
let field=field+1
|
|
|
|
let option=s:GetField(b:GPGOptions, ":", field)
|
|
|
|
endw
|
|
|
|
endi
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGEditOptions() {{{2
|
|
|
|
"
|
|
|
|
" create a scratch buffer with all recipients to add/remove recipients
|
|
|
|
"
|
|
|
|
fun s:GPGEditOptions()
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" only do this if it isn't already a GPGOptions_* buffer
|
2006-12-08 11:02:33 -05:00
|
|
|
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" save buffer name
|
2006-12-14 05:03:19 -05:00
|
|
|
let buffername=bufname("%")
|
2002-04-24 04:59:12 -04:00
|
|
|
let editbuffername="GPGOptions_" . buffername
|
|
|
|
|
|
|
|
" check if this buffer exists
|
2006-12-14 05:03:19 -05:00
|
|
|
if (!bufexists(editbuffername))
|
|
|
|
" create scratch buffer
|
|
|
|
exe 'silent! split ' . escape(editbuffername, ' *?\"'."'")
|
|
|
|
|
|
|
|
" add a autocommand to regenerate the options after a write
|
|
|
|
autocmd BufHidden,BufUnload <buffer> call s:GPGFinishOptionsBuffer()
|
|
|
|
else
|
|
|
|
if (bufwinnr(editbuffername) >= 0)
|
|
|
|
" switch to scratch buffer window
|
|
|
|
exe 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
|
|
|
|
else
|
|
|
|
" split scratch buffer window
|
|
|
|
exe 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'")
|
2006-12-14 05:51:22 -05:00
|
|
|
|
|
|
|
" add a autocommand to regenerate the options after a write
|
|
|
|
autocmd BufHidden,BufUnload <buffer> call s:GPGFinishOptionsBuffer()
|
2006-12-14 05:03:19 -05:00
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" empty the buffer
|
|
|
|
silent normal! 1GdG
|
|
|
|
endi
|
|
|
|
|
|
|
|
" Mark the buffer as a scratch buffer
|
|
|
|
setlocal buftype=nofile
|
|
|
|
setlocal noswapfile
|
|
|
|
setlocal nowrap
|
|
|
|
setlocal nobuflisted
|
|
|
|
setlocal nonumber
|
|
|
|
|
|
|
|
" so we know for which other buffer this edit buffer is
|
|
|
|
let b:corresponding_to=buffername
|
|
|
|
|
|
|
|
" put some comments to the scratch buffer
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
silent put ='GPG: THERE IS NO CHECK OF THE ENTERED OPTIONS!'
|
|
|
|
silent put ='GPG: YOU NEED TO KNOW WHAT YOU ARE DOING!'
|
|
|
|
silent put ='GPG: IF IN DOUBT, QUICKLY EXIT USING :x OR :bd'
|
|
|
|
silent put ='GPG: Please edit the list of options, one option per line'
|
|
|
|
silent put ='GPG: Please refer to the gpg documentation for valid options'
|
|
|
|
silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically'
|
2006-12-14 05:03:19 -05:00
|
|
|
silent put ='GPG: Closing this buffer commits changes'
|
2002-04-24 04:59:12 -04:00
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
|
|
|
|
" put the options in the scratch buffer
|
|
|
|
let options=getbufvar(b:corresponding_to, "GPGOptions")
|
|
|
|
let field=0
|
|
|
|
|
|
|
|
let option=s:GetField(options, ":", field)
|
|
|
|
while (strlen(option) > 0)
|
|
|
|
silent put =option
|
|
|
|
|
|
|
|
let field=field+1
|
|
|
|
let option=s:GetField(options, ":", field)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" delete the empty first line
|
|
|
|
silent normal! 1Gdd
|
|
|
|
|
|
|
|
" jump to the first option
|
2006-12-13 09:58:58 -05:00
|
|
|
silent normal! G
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" define highlight
|
|
|
|
if (has("syntax") && exists("g:syntax_on"))
|
|
|
|
syntax match GPGComment "^GPG:.*$"
|
|
|
|
highlight clear GPGComment
|
|
|
|
highlight link GPGComment Comment
|
|
|
|
endi
|
|
|
|
endi
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGFinishOptionsBuffer() {{{2
|
|
|
|
"
|
|
|
|
" create a new option list from OptionsBuffer
|
|
|
|
fun s:GPGFinishOptionsBuffer()
|
2006-12-13 10:28:31 -05:00
|
|
|
" guard for unencrypted files
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
echohl GPGWarning
|
|
|
|
echo "File is not encrypted, all GPG functions disabled!"
|
|
|
|
echohl None
|
|
|
|
return
|
|
|
|
endi
|
|
|
|
|
2006-12-14 05:51:22 -05:00
|
|
|
" go to buffer before doing work
|
|
|
|
if (bufnr("%") != expand("<abuf>"))
|
|
|
|
" switch to scratch buffer window
|
|
|
|
exe 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
|
|
|
|
endi
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" clear GPGOptions and GPGUnknownOptions
|
|
|
|
let GPGOptions=""
|
|
|
|
let GPGUnknownOptions=""
|
|
|
|
|
|
|
|
" delete the autocommand
|
2006-12-14 05:03:19 -05:00
|
|
|
autocmd! * <buffer>
|
2006-12-14 05:51:22 -05:00
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
let currentline=1
|
|
|
|
let option=getline(currentline)
|
|
|
|
|
|
|
|
" get the options from the scratch buffer
|
|
|
|
while (currentline <= line("$"))
|
|
|
|
" delete all spaces at beginning and end of the line
|
|
|
|
" also delete a '!' at the beginning of the line
|
|
|
|
let option=substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
|
|
|
|
" delete comment lines
|
|
|
|
let option=substitute(option, "^GPG:.*$", "", "")
|
|
|
|
|
|
|
|
" only do this if the line is not empty
|
|
|
|
if (strlen(option) > 0)
|
|
|
|
let GPGOptions=GPGOptions . option . ":"
|
|
|
|
endi
|
|
|
|
|
|
|
|
let currentline=currentline+1
|
|
|
|
let option=getline(currentline)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" write back the new option list to the corresponding buffer and mark it
|
|
|
|
" as modified
|
|
|
|
call setbufvar(b:corresponding_to, "GPGOptions", GPGOptions)
|
|
|
|
call setbufvar(b:corresponding_to, "&mod", 1)
|
|
|
|
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGNameToID(name) {{{2
|
|
|
|
"
|
|
|
|
" find GPG key ID corresponding to a name
|
|
|
|
" Returns: ID for the given name
|
|
|
|
fun s:GPGNameToID(name)
|
|
|
|
" ask gpg for the id for a name
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shellredir=s:shellredir
|
|
|
|
let &shell=s:shell
|
2006-11-16 10:29:55 -05:00
|
|
|
let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\"")
|
2007-01-29 08:38:16 -05:00
|
|
|
let &shellredir=s:shellredirsave
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shell=s:shellsave
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" parse the output of gpg
|
|
|
|
let pub_seen=0
|
|
|
|
let uid_seen=0
|
|
|
|
let line=0
|
|
|
|
let counter=0
|
|
|
|
let gpgids=""
|
|
|
|
let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n"
|
|
|
|
let linecontent=s:GetField(output, "\n", line)
|
|
|
|
while (strlen(linecontent))
|
|
|
|
" search for the next uid
|
|
|
|
if (pub_seen == 1)
|
|
|
|
if (s:GetField(linecontent, ":", 0) == "uid")
|
|
|
|
if (uid_seen == 0)
|
|
|
|
let choices=choices . counter . ": " . s:GetField(linecontent, ":", 9) . "\n"
|
|
|
|
let counter=counter+1
|
|
|
|
let uid_seen=1
|
|
|
|
else
|
|
|
|
let choices=choices . " " . s:GetField(linecontent, ":", 9) . "\n"
|
|
|
|
endi
|
|
|
|
else
|
|
|
|
let uid_seen=0
|
|
|
|
let pub_seen=0
|
|
|
|
endi
|
|
|
|
endi
|
|
|
|
|
|
|
|
" search for the next pub
|
|
|
|
if (pub_seen == 0)
|
|
|
|
if (s:GetField(linecontent, ":", 0) == "pub")
|
|
|
|
let gpgids=gpgids . s:GetField(linecontent, ":", 4) . ":"
|
|
|
|
let pub_seen=1
|
|
|
|
endi
|
|
|
|
endi
|
|
|
|
|
|
|
|
let line=line+1
|
|
|
|
let linecontent=s:GetField(output, "\n", line)
|
|
|
|
endw
|
|
|
|
|
|
|
|
" counter > 1 means we have more than one results
|
|
|
|
let answer=0
|
|
|
|
if (counter > 1)
|
|
|
|
let choices=choices . "Enter number: "
|
|
|
|
let answer=input(choices, "0")
|
|
|
|
while (answer == "")
|
|
|
|
let answer=input("Enter number: ", "0")
|
|
|
|
endw
|
|
|
|
endi
|
|
|
|
|
|
|
|
return s:GetField(gpgids, ":", answer)
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GPGIDToName(identity) {{{2
|
|
|
|
"
|
|
|
|
" find name corresponding to a GPG key ID
|
|
|
|
" Returns: Name for the given ID
|
|
|
|
fun s:GPGIDToName(identity)
|
|
|
|
" TODO is the encryption subkey really unique?
|
|
|
|
|
|
|
|
" ask gpg for the id for a name
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shellredir=s:shellredir
|
|
|
|
let &shell=s:shell
|
2006-11-16 10:29:55 -05:00
|
|
|
let output=system(s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity )
|
2007-01-29 08:38:16 -05:00
|
|
|
let &shellredir=s:shellredirsave
|
2006-12-08 05:27:58 -05:00
|
|
|
let &shell=s:shellsave
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
" parse the output of gpg
|
|
|
|
let pub_seen=0
|
|
|
|
let finish=0
|
|
|
|
let line=0
|
|
|
|
let linecontent=s:GetField(output, "\n", line)
|
|
|
|
while (strlen(linecontent) && !finish)
|
|
|
|
if (pub_seen == 0) " search for the next pub
|
|
|
|
if (s:GetField(linecontent, ":", 0) == "pub")
|
|
|
|
let pub_seen=1
|
|
|
|
endi
|
|
|
|
else " search for the next uid
|
|
|
|
if (s:GetField(linecontent, ":", 0) == "uid")
|
|
|
|
let pub_seen=0
|
|
|
|
let finish=1
|
|
|
|
let uid=s:GetField(linecontent, ":", 9)
|
|
|
|
endi
|
|
|
|
endi
|
|
|
|
|
|
|
|
let line=line+1
|
|
|
|
let linecontent=s:GetField(output, "\n", line)
|
|
|
|
endw
|
|
|
|
|
|
|
|
return uid
|
|
|
|
endf
|
|
|
|
|
|
|
|
" Function: s:GetField(line, separator, field) {{{2
|
|
|
|
"
|
|
|
|
" find field of 'separator' separated string, counting starts with 0
|
|
|
|
" Returns: content of the field, if field doesn't exist it returns an empty
|
|
|
|
" string
|
|
|
|
fun s:GetField(line, separator, field)
|
|
|
|
let counter=a:field
|
|
|
|
let separatorLength=strlen(a:separator)
|
|
|
|
let start=0
|
|
|
|
let end=match(a:line, a:separator)
|
|
|
|
if (end < 0)
|
|
|
|
let end=strlen(a:line)
|
|
|
|
endi
|
|
|
|
|
|
|
|
" search for requested field
|
|
|
|
while (start < strlen(a:line) && counter > 0)
|
|
|
|
let counter=counter-separatorLength
|
|
|
|
let start=end+separatorLength
|
|
|
|
let end=match(a:line, a:separator, start)
|
|
|
|
if (end < 0)
|
|
|
|
let end=strlen(a:line)
|
|
|
|
endi
|
|
|
|
endw
|
|
|
|
|
|
|
|
if (start < strlen(a:line))
|
|
|
|
return strpart(a:line, start, end-start)
|
|
|
|
else
|
|
|
|
return ""
|
|
|
|
endi
|
|
|
|
endf
|
2011-07-05 02:51:02 -04:00
|
|
|
|
|
|
|
" Function: s:GPGDebug(level, text) {{{2
|
|
|
|
"
|
|
|
|
" output debug message, if this message has high enough importance
|
|
|
|
fun s:GPGDebug(level, text)
|
|
|
|
if (g:GPGDebugLevel >= a:level)
|
|
|
|
echom a:text
|
|
|
|
endi
|
|
|
|
endf
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
" Section: Command definitions {{{1
|
|
|
|
com! GPGViewRecipients call s:GPGViewRecipients()
|
|
|
|
com! GPGEditRecipients call s:GPGEditRecipients()
|
|
|
|
com! GPGViewOptions call s:GPGViewOptions()
|
|
|
|
com! GPGEditOptions call s:GPGEditOptions()
|
|
|
|
|
2011-07-05 02:56:18 -04:00
|
|
|
" vim600: foldmethod=marker:foldlevel=0
|