2010-01-27 08:18:04 -05:00
|
|
|
|
" Name: gnupg.vim
|
2015-12-04 19:20:48 -05:00
|
|
|
|
" Last Change: 2015 Dec 04
|
2011-11-23 15:30:35 -05:00
|
|
|
|
" Maintainer: James McCoy <vega.james@gmail.com>
|
2011-05-24 07:28:02 -04:00
|
|
|
|
" Original Author: Markus Braun <markus.braun@krawel.de>
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Summary: Vim plugin for transparent editing of gpg encrypted files.
|
2011-05-24 07:28:02 -04:00
|
|
|
|
" License: This program is free software; you can redistribute it and/or
|
|
|
|
|
" modify it under the terms of the GNU General Public License
|
|
|
|
|
" as published by the Free Software Foundation; either version
|
|
|
|
|
" 2 of the License, or (at your option) any later version.
|
|
|
|
|
" See http://www.gnu.org/copyleft/gpl-2.0.txt
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
2003-05-30 09:29:16 -04:00
|
|
|
|
" Section: Documentation {{{1
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
|
|
|
|
" Description: {{{2
|
2009-05-27 07:10:20 -04:00
|
|
|
|
"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" This script implements transparent editing of gpg encrypted files. The
|
|
|
|
|
" filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such
|
2003-06-24 07:57:16 -04:00
|
|
|
|
" 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
|
2012-05-30 22:15:46 -04:00
|
|
|
|
" viminfo, swapfile, and undofile to increase security.
|
2003-05-30 09:29:16 -04:00
|
|
|
|
"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Installation: {{{2
|
2003-05-30 09:29:16 -04:00
|
|
|
|
"
|
|
|
|
|
" 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.
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" From "man 1 gpg-agent":
|
|
|
|
|
"
|
|
|
|
|
" ...
|
|
|
|
|
" You should always add the following lines to your .bashrc or whatever
|
|
|
|
|
" initialization file is used for all shell invocations:
|
|
|
|
|
"
|
2009-05-27 07:10:20 -04:00
|
|
|
|
" GPG_TTY=`tty`
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" export GPG_TTY
|
|
|
|
|
"
|
|
|
|
|
" It is important that this environment variable always reflects the out‐
|
|
|
|
|
" put of the tty command. For W32 systems this option is not required.
|
|
|
|
|
" ...
|
|
|
|
|
"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Most distributions provide software to ease handling of gpg and gpg-agent.
|
|
|
|
|
" Examples are keychain or seahorse.
|
|
|
|
|
"
|
2013-04-23 22:15:45 -04:00
|
|
|
|
" If there are specific actions that should take place when editing a
|
|
|
|
|
" GnuPG-managed buffer, an autocmd for the User event and GnuPG pattern can
|
|
|
|
|
" be defined. For example, the following will set 'textwidth' to 72 for all
|
|
|
|
|
" GnuPG-encrypted buffers:
|
|
|
|
|
"
|
|
|
|
|
" autocmd User GnuPG setl textwidth=72
|
|
|
|
|
"
|
|
|
|
|
" This will be triggered before any BufRead or BufNewFile autocmds, and
|
|
|
|
|
" therefore will not take precedence over settings specific to any filetype
|
|
|
|
|
" that may get set.
|
|
|
|
|
"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Commands: {{{2
|
2003-05-30 09:29:16 -04:00
|
|
|
|
"
|
|
|
|
|
" :GPGEditRecipients
|
|
|
|
|
" Opens a scratch buffer to change the list of recipients. Recipients that
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" are unknown (not in your public key) are highlighted and have
|
|
|
|
|
" a prepended "!". Closing the buffer makes the changes permanent.
|
2003-05-30 09:29:16 -04:00
|
|
|
|
"
|
|
|
|
|
" :GPGViewRecipients
|
|
|
|
|
" Prints the list of recipients.
|
|
|
|
|
"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" :GPGEditOptions
|
|
|
|
|
" Opens a scratch buffer to change the options for encryption (symmetric,
|
|
|
|
|
" asymmetric, signing). Closing the buffer makes the changes permanent.
|
|
|
|
|
" WARNING: There is no check of the entered options, so you need to know
|
|
|
|
|
" what you are doing.
|
|
|
|
|
"
|
|
|
|
|
" :GPGViewOptions
|
|
|
|
|
" Prints the list of options.
|
|
|
|
|
"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Variables: {{{2
|
2006-12-15 13:09:40 -05:00
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" g:GPGExecutable
|
|
|
|
|
" If set used as gpg executable, otherwise the system chooses what is run
|
2015-10-03 10:26:29 -04:00
|
|
|
|
" when "gpg" is called. Defaults to "gpg --trust-model always".
|
2008-07-31 11:43:14 -04:00
|
|
|
|
"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" g:GPGUseAgent
|
|
|
|
|
" If set to 0 a possible available gpg-agent won't be used. Defaults to 1.
|
|
|
|
|
"
|
2007-03-01 09:58:04 -05:00
|
|
|
|
" g:GPGPreferSymmetric
|
|
|
|
|
" If set to 1 symmetric encryption is preferred for new files. Defaults to 0.
|
|
|
|
|
"
|
|
|
|
|
" g:GPGPreferArmor
|
2010-02-16 07:56:18 -05:00
|
|
|
|
" If set to 1 armored data is preferred for new files. Defaults to 0
|
|
|
|
|
" unless a "*.asc" file is being edited.
|
2007-03-01 09:58:04 -05:00
|
|
|
|
"
|
2009-05-27 07:10:20 -04:00
|
|
|
|
" g:GPGPreferSign
|
|
|
|
|
" If set to 1 signed data is preferred for new files. Defaults to 0.
|
|
|
|
|
"
|
2008-08-15 12:50:33 -04:00
|
|
|
|
" 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.
|
|
|
|
|
"
|
2014-10-29 11:53:59 -04:00
|
|
|
|
" g:GPGPossibleRecipients
|
2014-10-09 10:48:03 -04:00
|
|
|
|
" If set, these contents are loaded into the recipients dialog. This
|
|
|
|
|
" allows to add commented lines with possible recipients to the list,
|
2015-10-03 10:26:29 -04:00
|
|
|
|
" which can be uncommented to select the actual recipients. Default is
|
|
|
|
|
" unset. Example:
|
2014-10-09 10:48:03 -04:00
|
|
|
|
"
|
2014-10-29 11:53:59 -04:00
|
|
|
|
" let g:GPGPossibleRecipients=[
|
|
|
|
|
" \"Example User <example@example.com>",
|
|
|
|
|
" \"Other User <otherexample@example.com>"
|
|
|
|
|
" \]
|
2014-10-09 10:48:03 -04:00
|
|
|
|
"
|
|
|
|
|
"
|
2011-08-01 20:44:17 -04:00
|
|
|
|
" g:GPGUsePipes
|
|
|
|
|
" If set to 1, use pipes instead of temporary files when interacting with
|
|
|
|
|
" gnupg. When set to 1, this can cause terminal-based gpg agents to not
|
|
|
|
|
" display correctly when prompting for passwords. Defaults to 0.
|
|
|
|
|
"
|
2011-11-23 17:09:42 -05:00
|
|
|
|
" g:GPGHomedir
|
|
|
|
|
" If set, specifies the directory that will be used for GPG's homedir.
|
|
|
|
|
" This corresponds to gpg's --homedir option. This variable is a Vim
|
2015-10-03 10:26:29 -04:00
|
|
|
|
" string. Default is unset.
|
2011-11-23 17:09:42 -05:00
|
|
|
|
"
|
2013-02-10 11:59:44 -05:00
|
|
|
|
" g:GPGFilePattern
|
|
|
|
|
" If set, overrides the default set of file patterns that determine
|
|
|
|
|
" whether this plugin will be activated. Defaults to
|
|
|
|
|
" '*.\(gpg\|asc\|pgp\)'.
|
|
|
|
|
"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Known Issues: {{{2
|
2009-05-27 07:10:20 -04:00
|
|
|
|
"
|
2010-02-16 07:56:18 -05:00
|
|
|
|
" In some cases gvim can't decrypt files
|
2009-05-27 07:10:20 -04:00
|
|
|
|
|
|
|
|
|
" This is caused by the fact that a running gvim has no TTY and thus gpg is
|
|
|
|
|
" not able to ask for the passphrase by itself. This is a problem for Windows
|
|
|
|
|
" and Linux versions of gvim and could not be solved unless a "terminal
|
|
|
|
|
" emulation" is implemented for gvim. To circumvent this you have to use any
|
|
|
|
|
" combination of gpg-agent and a graphical pinentry program:
|
|
|
|
|
"
|
|
|
|
|
" - gpg-agent only:
|
|
|
|
|
" you need to provide the passphrase for the needed key to gpg-agent
|
|
|
|
|
" in a terminal before you open files with gvim which require this key.
|
|
|
|
|
"
|
|
|
|
|
" - pinentry only:
|
|
|
|
|
" you will get a popup window every time you open a file that needs to
|
|
|
|
|
" be decrypted.
|
|
|
|
|
"
|
|
|
|
|
" - gpgagent and pinentry:
|
|
|
|
|
" you will get a popup window the first time you open a file that
|
|
|
|
|
" needs to be decrypted.
|
|
|
|
|
"
|
2015-12-04 19:20:48 -05:00
|
|
|
|
" If you're using Vim <7.4.959, after the plugin runs any external command,
|
|
|
|
|
" Vim will no longer be able to yank to/paste from the X clipboard or
|
|
|
|
|
" primary selections. This is caused by a workaround for a different bug
|
|
|
|
|
" where Vim no longer recognizes the key codes for keys such as the arrow
|
|
|
|
|
" keys after running GnuPG. See the discussion at
|
|
|
|
|
" https://github.com/jamessan/vim-gnupg/issues/36 for more details.
|
|
|
|
|
"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Credits: {{{2
|
2009-05-27 07:10:20 -04:00
|
|
|
|
"
|
|
|
|
|
" - Mathieu Clabaut for inspirations through his vimspell.vim script.
|
|
|
|
|
" - Richard Bronosky for patch to enable ".pgp" suffix.
|
|
|
|
|
" - Erik Remmelzwaal for patch to enable windows support and patient beta
|
|
|
|
|
" testing.
|
|
|
|
|
" - Lars Becker for patch to make gpg2 working.
|
2010-02-16 07:56:18 -05:00
|
|
|
|
" - Thomas Arendsen Hein for patch to convert encoding of gpg output.
|
2009-05-27 07:10:20 -04:00
|
|
|
|
" - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model
|
|
|
|
|
" and patient beta testing.
|
|
|
|
|
" - Giel van Schijndel for patch to get GPG_TTY dynamically.
|
|
|
|
|
" - Sebastian Luettich for patch to fix issue with symmetric encryption an set
|
|
|
|
|
" recipients.
|
2010-02-16 07:56:18 -05:00
|
|
|
|
" - Tim Swast for patch to generate signed files.
|
|
|
|
|
" - James Vega for patches for better '*.asc' handling, better filename
|
|
|
|
|
" escaping and better handling of multiple keyrings.
|
2006-12-15 13:09:40 -05:00
|
|
|
|
"
|
2003-05-30 09:29:16 -04:00
|
|
|
|
" Section: Plugin header {{{1
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" guard against multiple loads {{{2
|
2012-05-31 00:31:56 -04:00
|
|
|
|
if (exists("g:loaded_gnupg") || &cp || exists("#GnuPG"))
|
2006-12-15 13:09:40 -05:00
|
|
|
|
finish
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2012-05-31 22:27:42 -04:00
|
|
|
|
let g:loaded_gnupg = '2.5'
|
2010-10-28 01:30:47 -04:00
|
|
|
|
let s:GPGInitRun = 0
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" check for correct vim version {{{2
|
2011-05-24 01:16:05 -04:00
|
|
|
|
if (v:version < 702)
|
|
|
|
|
echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.2' | echohl None
|
2010-02-16 07:56:18 -05:00
|
|
|
|
finish
|
|
|
|
|
endif
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Section: Autocmd setup {{{1
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2013-02-05 04:38:25 -05:00
|
|
|
|
if (!exists("g:GPGFilePattern"))
|
|
|
|
|
let g:GPGFilePattern = '*.\(gpg\|asc\|pgp\)'
|
|
|
|
|
endif
|
|
|
|
|
|
2003-05-30 09:29:16 -04:00
|
|
|
|
augroup GnuPG
|
2008-07-31 11:43:14 -04:00
|
|
|
|
autocmd!
|
|
|
|
|
|
|
|
|
|
" do the decryption
|
2013-02-05 04:38:25 -05:00
|
|
|
|
exe "autocmd BufReadCmd " . g:GPGFilePattern . " call s:GPGInit(1) |" .
|
2015-10-03 10:27:59 -04:00
|
|
|
|
\ " call s:GPGDecrypt(1)"
|
2013-02-05 04:38:25 -05:00
|
|
|
|
exe "autocmd FileReadCmd " . g:GPGFilePattern . " call s:GPGInit(0) |" .
|
|
|
|
|
\ " call s:GPGDecrypt(0)"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
|
|
|
|
" convert all text to encrypted text before writing
|
2013-02-22 14:51:18 -05:00
|
|
|
|
" We check for GPGCorrespondingTo to avoid triggering on writes in GPG Options/Recipient windows
|
|
|
|
|
exe "autocmd BufWriteCmd,FileWriteCmd " . g:GPGFilePattern . " if !exists('b:GPGCorrespondingTo') |" .
|
2014-12-25 15:11:31 -05:00
|
|
|
|
\ " call s:GPGInit(0) |" .
|
|
|
|
|
\ " call s:GPGEncrypt() |" .
|
|
|
|
|
\ " endif"
|
2008-08-15 12:50:33 -04:00
|
|
|
|
|
|
|
|
|
" cleanup on leaving vim
|
2013-02-05 04:38:25 -05:00
|
|
|
|
exe "autocmd VimLeave " . g:GPGFilePattern . " call s:GPGCleanup()"
|
2003-05-30 09:29:16 -04:00
|
|
|
|
augroup END
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2009-06-08 12:32:13 -04:00
|
|
|
|
" Section: Constants {{{1
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2009-06-08 12:32:13 -04:00
|
|
|
|
let s:GPGMagicString = "\t \t"
|
2012-05-30 23:48:24 -04:00
|
|
|
|
let s:keyPattern = '\%(0x\)\=[[:xdigit:]]\{8,16}'
|
2009-06-08 12:32:13 -04:00
|
|
|
|
|
2003-05-30 09:29:16 -04:00
|
|
|
|
" Section: Highlight setup {{{1
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
highlight default link GPGWarning WarningMsg
|
|
|
|
|
highlight default link GPGError ErrorMsg
|
|
|
|
|
highlight default link GPGHighlightUnknownRecipient ErrorMsg
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2003-05-30 09:29:16 -04:00
|
|
|
|
" Section: Functions {{{1
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2015-09-29 19:48:18 -04:00
|
|
|
|
" Function: s:shellescape(s[, special]) {{{2
|
|
|
|
|
"
|
|
|
|
|
" Calls shellescape(), also taking into account 'shellslash'
|
|
|
|
|
" when on Windows and using $COMSPEC as the shell.
|
|
|
|
|
"
|
|
|
|
|
" Returns: shellescaped string
|
|
|
|
|
"
|
|
|
|
|
function s:shellescape(s, ...)
|
|
|
|
|
let special = a:0 ? a:1 : 0
|
|
|
|
|
if exists('+shellslash') && &shell == $COMSPEC
|
|
|
|
|
let ssl = &shellslash
|
|
|
|
|
set noshellslash
|
|
|
|
|
|
|
|
|
|
let escaped = shellescape(a:s, special)
|
|
|
|
|
|
|
|
|
|
let &shellslash = ssl
|
|
|
|
|
else
|
|
|
|
|
let escaped = shellescape(a:s, special)
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
return escaped
|
|
|
|
|
endfunction
|
|
|
|
|
|
2012-05-30 22:15:46 -04:00
|
|
|
|
" Function: s:GPGInit(bufread) {{{2
|
2006-12-15 13:09:40 -05:00
|
|
|
|
"
|
|
|
|
|
" initialize the plugin
|
2012-05-30 22:15:46 -04:00
|
|
|
|
" The bufread argument specifies whether this was called due to BufReadCmd
|
2006-12-15 13:09:40 -05:00
|
|
|
|
"
|
2012-05-30 22:15:46 -04:00
|
|
|
|
function s:GPGInit(bufread)
|
|
|
|
|
call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGInit(%d)", a:bufread))
|
2011-06-26 15:40:34 -04:00
|
|
|
|
|
2012-05-30 22:17:05 -04:00
|
|
|
|
" For FileReadCmd, we're reading the contents into another buffer. If that
|
|
|
|
|
" buffer is also destined to be encrypted, then these settings will have
|
|
|
|
|
" already been set, otherwise don't set them since it limits the
|
|
|
|
|
" functionality of the cleartext buffer.
|
|
|
|
|
if a:bufread
|
|
|
|
|
" we don't want a swap file, as it writes unencrypted data to disk
|
|
|
|
|
setl noswapfile
|
|
|
|
|
|
|
|
|
|
" if persistent undo is present, disable it for this buffer
|
|
|
|
|
if exists('+undofile')
|
|
|
|
|
setl noundofile
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
" first make sure nothing is written to ~/.viminfo while editing
|
|
|
|
|
" an encrypted file.
|
|
|
|
|
set viminfo=
|
2011-06-26 15:40:34 -04:00
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
" the rest only has to be run once
|
2010-10-28 01:30:47 -04:00
|
|
|
|
if s:GPGInitRun
|
|
|
|
|
return
|
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" check what gpg command to use
|
|
|
|
|
if (!exists("g:GPGExecutable"))
|
|
|
|
|
let g:GPGExecutable = "gpg --trust-model always"
|
|
|
|
|
endif
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" check if gpg-agent is allowed
|
|
|
|
|
if (!exists("g:GPGUseAgent"))
|
|
|
|
|
let g:GPGUseAgent = 1
|
|
|
|
|
endif
|
|
|
|
|
|
2007-03-01 09:58:04 -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"))
|
2010-02-16 07:56:18 -05:00
|
|
|
|
" .asc files should be armored as that's what the extension is used for
|
|
|
|
|
if expand('<afile>') =~ '\.asc$'
|
|
|
|
|
let g:GPGPreferArmor = 1
|
|
|
|
|
else
|
|
|
|
|
let g:GPGPreferArmor = 0
|
|
|
|
|
endif
|
2007-03-01 09:58:04 -05:00
|
|
|
|
endif
|
|
|
|
|
|
2009-05-27 07:10:20 -04:00
|
|
|
|
" check if signed files are preferred
|
|
|
|
|
if (!exists("g:GPGPreferSign"))
|
|
|
|
|
let g:GPGPreferSign = 0
|
|
|
|
|
endif
|
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" start with empty default recipients if none is defined so far
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (!exists("g:GPGDefaultRecipients"))
|
|
|
|
|
let g:GPGDefaultRecipients = []
|
|
|
|
|
endif
|
|
|
|
|
|
2014-10-29 11:53:59 -04:00
|
|
|
|
if (!exists("g:GPGPossibleRecipients"))
|
|
|
|
|
let g:GPGPossibleRecipients = []
|
2014-10-09 10:48:03 -04:00
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
|
2011-08-01 20:44:17 -04:00
|
|
|
|
" prefer not to use pipes since it can garble gpg agent display
|
|
|
|
|
if (!exists("g:GPGUsePipes"))
|
|
|
|
|
let g:GPGUsePipes = 0
|
|
|
|
|
endif
|
|
|
|
|
|
2011-11-23 15:35:01 -05:00
|
|
|
|
" allow alternate gnupg homedir
|
|
|
|
|
if (!exists('g:GPGHomedir'))
|
|
|
|
|
let g:GPGHomedir = ''
|
|
|
|
|
endif
|
|
|
|
|
|
2008-01-23 09:49:33 -05:00
|
|
|
|
" print version
|
|
|
|
|
call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg)
|
|
|
|
|
|
2015-07-16 23:13:12 -04:00
|
|
|
|
let s:GPGCommand = g:GPGExecutable
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2010-02-16 07:56:18 -05:00
|
|
|
|
" don't use tty in gvim except for windows: we get their a tty for free.
|
2007-03-01 09:58:04 -05:00
|
|
|
|
" FIXME find a better way to avoid an error.
|
|
|
|
|
" with this solution only --use-agent will work
|
2010-02-16 07:56:18 -05:00
|
|
|
|
if (has("gui_running") && !has("gui_win32"))
|
2015-10-03 10:26:43 -04:00
|
|
|
|
let s:GPGCommand .= " --no-tty"
|
2007-03-01 09:58:04 -05:00
|
|
|
|
endif
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" setup shell environment for unix and windows
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let s:shellredirsave = &shellredir
|
|
|
|
|
let s:shellsave = &shell
|
2011-05-24 01:21:19 -04:00
|
|
|
|
let s:shelltempsave = &shelltemp
|
|
|
|
|
" noshelltemp isn't currently supported on Windows, but it doesn't cause any
|
|
|
|
|
" errors and this future proofs us against requiring changes if Windows
|
|
|
|
|
" gains noshelltemp functionality
|
2011-08-01 20:44:17 -04:00
|
|
|
|
let s:shelltemp = !g:GPGUsePipes
|
2009-05-27 07:10:20 -04:00
|
|
|
|
if (has("unix"))
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" unix specific settings
|
2010-01-27 08:18:04 -05:00
|
|
|
|
let s:shellredir = ">%s 2>&1"
|
|
|
|
|
let s:shell = '/bin/sh'
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let s:stderrredirnull = '2>/dev/null'
|
2009-05-27 07:10:20 -04:00
|
|
|
|
else
|
|
|
|
|
" windows specific settings
|
|
|
|
|
let s:shellredir = '>%s'
|
|
|
|
|
let s:shell = &shell
|
|
|
|
|
let s:stderrredirnull = '2>nul'
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "shellredirsave: " . s:shellredirsave)
|
|
|
|
|
call s:GPGDebug(3, "shellsave: " . s:shellsave)
|
2011-05-24 01:21:19 -04:00
|
|
|
|
call s:GPGDebug(3, "shelltempsave: " . s:shelltempsave)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "shell: " . s:shell)
|
|
|
|
|
call s:GPGDebug(3, "shellcmdflag: " . &shellcmdflag)
|
|
|
|
|
call s:GPGDebug(3, "shellxquote: " . &shellxquote)
|
|
|
|
|
call s:GPGDebug(3, "shellredir: " . s:shellredir)
|
|
|
|
|
call s:GPGDebug(3, "stderrredirnull: " . s:stderrredirnull)
|
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "shell implementation: " . resolve(s:shell))
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" find the supported algorithms
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let output = s:GPGSystem({ 'level': 2, 'args': '--version' })
|
2008-08-15 12:50:33 -04:00
|
|
|
|
|
2015-07-16 23:13:12 -04:00
|
|
|
|
let gpgversion = substitute(output, '^gpg (GnuPG) \([0-9]\+\.\d\+\).*', '\1', '')
|
2008-08-15 12:50:33 -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", "")
|
2010-01-27 08:18:04 -05:00
|
|
|
|
let s:GPGCompress = substitute(output, ".*Compress.\\{-}: \\(.\\{-}\\)\n.*", "\\1", "")
|
|
|
|
|
|
2015-07-16 23:13:12 -04:00
|
|
|
|
" determine if gnupg can use the gpg-agent
|
|
|
|
|
if (str2float(gpgversion) >= 2.1 || (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1))
|
|
|
|
|
if (!exists("$GPG_TTY") && !has("gui_running"))
|
|
|
|
|
" Need to determine the associated tty by running a command in the
|
|
|
|
|
" shell. We can't use system() here because that doesn't run in a shell
|
|
|
|
|
" connected to a tty, so it's rather useless.
|
|
|
|
|
"
|
|
|
|
|
" Save/restore &modified so the buffer isn't incorrectly marked as
|
|
|
|
|
" modified just by detecting the correct tty value.
|
|
|
|
|
" Do the &undolevels dance so the :read and :delete don't get added into
|
|
|
|
|
" the undo tree, as the user needn't be aware of these.
|
|
|
|
|
let [mod, levels] = [&l:modified, &undolevels]
|
|
|
|
|
set undolevels=-1
|
|
|
|
|
silent read !tty
|
|
|
|
|
let $GPG_TTY = getline('.')
|
|
|
|
|
silent delete
|
|
|
|
|
let [&l:modified, &undolevels] = [mod, levels]
|
|
|
|
|
" redraw is needed since we're using silent to run !tty, c.f. :help :!
|
|
|
|
|
redraw!
|
|
|
|
|
if (v:shell_error)
|
|
|
|
|
let $GPG_TTY = ""
|
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "$GPG_TTY is not set and the `tty` command failed! gpg-agent might not work."
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
|
|
|
|
endif
|
2015-10-03 10:26:43 -04:00
|
|
|
|
let s:GPGCommand .= " --use-agent"
|
2015-07-16 23:13:12 -04:00
|
|
|
|
else
|
2015-10-03 10:26:43 -04:00
|
|
|
|
let s:GPGCommand .= " --no-use-agent"
|
2015-07-16 23:13:12 -04:00
|
|
|
|
endif
|
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(2, "public key algorithms: " . s:GPGPubkey)
|
|
|
|
|
call s:GPGDebug(2, "cipher algorithms: " . s:GPGCipher)
|
|
|
|
|
call s:GPGDebug(2, "hashing algorithms: " . s:GPGHash)
|
|
|
|
|
call s:GPGDebug(2, "compression algorithms: " . s:GPGCompress)
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGInit()")
|
2010-10-28 01:30:47 -04:00
|
|
|
|
let s:GPGInitRun = 1
|
2008-08-15 12:50:33 -04:00
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
" Function: s:GPGCleanup() {{{2
|
|
|
|
|
"
|
|
|
|
|
" cleanup on leaving vim
|
|
|
|
|
"
|
|
|
|
|
function s:GPGCleanup()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCleanup()")
|
|
|
|
|
|
2008-08-15 12:50:33 -04:00
|
|
|
|
" wipe out screen
|
|
|
|
|
new +only
|
|
|
|
|
redraw!
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCleanup()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2012-05-30 22:15:46 -04:00
|
|
|
|
" Function: s:GPGDecrypt(bufread) {{{2
|
2003-05-30 09:29:16 -04:00
|
|
|
|
"
|
|
|
|
|
" decrypt the buffer and find all recipients of the encrypted file
|
2012-05-30 22:15:46 -04:00
|
|
|
|
" The bufread argument specifies whether this was called due to BufReadCmd
|
2003-05-30 09:29:16 -04:00
|
|
|
|
"
|
2012-05-30 22:15:46 -04:00
|
|
|
|
function s:GPGDecrypt(bufread)
|
|
|
|
|
call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGDecrypt(%d)", a:bufread))
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" get the filename of the current buffer
|
2010-10-28 01:30:47 -04:00
|
|
|
|
let filename = expand("<afile>:p")
|
|
|
|
|
|
2011-10-13 17:22:09 -04:00
|
|
|
|
" clear GPGRecipients and GPGOptions
|
2014-07-27 22:26:48 -04:00
|
|
|
|
if type(g:GPGDefaultRecipients) == type([])
|
|
|
|
|
let b:GPGRecipients = copy(g:GPGDefaultRecipients)
|
|
|
|
|
else
|
|
|
|
|
let b:GPGRecipients = []
|
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!"
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
2011-10-13 17:22:09 -04:00
|
|
|
|
let b:GPGOptions = []
|
|
|
|
|
|
2015-10-03 10:27:17 -04:00
|
|
|
|
" file name minus extension
|
|
|
|
|
let autocmd_filename = fnameescape(expand('<afile>:r'))
|
|
|
|
|
|
2011-06-26 15:40:34 -04:00
|
|
|
|
" File doesn't exist yet, so nothing to decrypt
|
2013-09-08 14:07:21 -04:00
|
|
|
|
if !filereadable(filename)
|
2013-04-23 22:15:45 -04:00
|
|
|
|
" Allow the user to define actions for GnuPG buffers
|
|
|
|
|
silent doautocmd User GnuPG
|
|
|
|
|
" call the autocommand for the file minus .gpg$
|
2015-10-03 10:27:17 -04:00
|
|
|
|
silent execute ':doautocmd BufNewFile ' . autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called BufNewFile autocommand for ' . autocmd_filename)
|
2013-02-22 14:39:38 -05:00
|
|
|
|
|
|
|
|
|
" This is a new file, so force the user to edit the recipient list if
|
|
|
|
|
" they open a new file and public keys are preferred
|
2015-10-03 10:28:35 -04:00
|
|
|
|
if (g:GPGPreferSymmetric == 0)
|
2013-02-22 14:39:38 -05:00
|
|
|
|
call s:GPGEditRecipients()
|
|
|
|
|
endif
|
|
|
|
|
|
2010-10-28 01:30:47 -04:00
|
|
|
|
return
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2011-10-13 17:22:09 -04:00
|
|
|
|
" Only let this if the file actually exists, otherwise GPG functionality
|
|
|
|
|
" will be disabled when editing a buffer that doesn't yet have a backing
|
|
|
|
|
" file
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGEncrypted = 0
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" find the recipients of the file
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let cmd = { 'level': 3 }
|
2015-09-29 19:48:18 -04:00
|
|
|
|
let cmd.args = '--verbose --decrypt --list-only --dry-run --no-use-agent --logger-fd 1 ' . s:shellescape(filename)
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let output = s:GPGSystem(cmd)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2012-05-30 22:18:01 -04:00
|
|
|
|
" Suppress the "N more lines" message when editing a file, not when reading
|
|
|
|
|
" the contents of a file into a buffer
|
|
|
|
|
let silent = a:bufread ? 'silent ' : ''
|
|
|
|
|
|
2012-05-30 23:48:24 -04:00
|
|
|
|
let asymmPattern = 'gpg: public key is ' . s:keyPattern
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" check if the file is symmetric/asymmetric encrypted
|
2008-01-23 09:49:33 -05:00
|
|
|
|
if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" file is symmetric encrypted
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGEncrypted = 1
|
2008-01-23 09:49:33 -05:00
|
|
|
|
call s:GPGDebug(1, "this file is symmetric encrypted")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["symmetric"]
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" find the used cipher algorithm
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let cipher = substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["cipher-algo " . cipher]
|
2008-01-23 09:49:33 -05:00
|
|
|
|
call s:GPGDebug(1, "cipher-algo is " . cipher)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "The cipher " . cipher . " is not known by the local gpg command. Using default!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echo
|
|
|
|
|
echohl None
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2011-08-13 18:00:27 -04:00
|
|
|
|
elseif (match(output, asymmPattern) >= 0)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" file is asymmetric encrypted
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGEncrypted = 1
|
2008-01-23 09:49:33 -05:00
|
|
|
|
call s:GPGDebug(1, "this file is asymmetric encrypted")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["encrypt"]
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" find the used public keys
|
2011-08-13 18:00:27 -04:00
|
|
|
|
let start = match(output, asymmPattern)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
while (start >= 0)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let start = start + strlen("gpg: public key is ")
|
2012-05-30 23:48:24 -04:00
|
|
|
|
let recipient = matchstr(output, s:keyPattern, start)
|
2008-01-23 09:49:33 -05:00
|
|
|
|
call s:GPGDebug(1, "recipient is " . recipient)
|
2015-11-04 21:48:25 -05:00
|
|
|
|
" In order to support anonymous communication, GnuPG allows eliding
|
|
|
|
|
" information in the encryption metadata specifying what keys the file
|
|
|
|
|
" was encrypted to (c.f., --throw-keyids and --hidden-recipient). In
|
|
|
|
|
" that case, the recipient(s) will be listed as having used a key of all
|
|
|
|
|
" zeroes.
|
|
|
|
|
" Since this will obviously never actually be in a keyring, only try to
|
|
|
|
|
" convert to an ID or add to the recipients list if it's not a hidden
|
|
|
|
|
" recipient.
|
|
|
|
|
if recipient !~? '^0x0\+$'
|
|
|
|
|
let name = s:GPGNameToID(recipient)
|
|
|
|
|
if !empty(name)
|
|
|
|
|
let b:GPGRecipients += [name]
|
|
|
|
|
call s:GPGDebug(1, "name of recipient is " . name)
|
|
|
|
|
else
|
|
|
|
|
let b:GPGRecipients += [recipient]
|
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
|
|
|
|
|
echohl None
|
|
|
|
|
end
|
2006-12-15 13:09:40 -05:00
|
|
|
|
end
|
2011-08-13 18:00:27 -04:00
|
|
|
|
let start = match(output, asymmPattern, start)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endwhile
|
2008-01-23 09:49:33 -05:00
|
|
|
|
else
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" file is not encrypted
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGEncrypted = 0
|
2008-01-23 09:49:33 -05:00
|
|
|
|
call s:GPGDebug(1, "this file is not encrypted")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2012-05-30 22:18:01 -04:00
|
|
|
|
exe printf('%sr %s', silent, fnameescape(filename))
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2015-01-05 22:36:31 -05:00
|
|
|
|
if a:bufread
|
2015-10-03 10:27:17 -04:00
|
|
|
|
silent execute ':doautocmd BufReadPre ' . autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called BufReadPre autocommand for ' . autocmd_filename)
|
2015-01-05 22:36:31 -05:00
|
|
|
|
else
|
2015-10-03 10:27:17 -04:00
|
|
|
|
silent execute ':doautocmd FileReadPre ' . autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called FileReadPre autocommand for ' . autocmd_filename)
|
2015-01-05 22:36:31 -05:00
|
|
|
|
endif
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" check if the message is armored
|
2008-01-23 09:49:33 -05:00
|
|
|
|
if (match(output, "gpg: armor header") >= 0)
|
|
|
|
|
call s:GPGDebug(1, "this file is armored")
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["armor"]
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" finally decrypt the buffer content
|
|
|
|
|
" since even with the --quiet option passphrase typos will be reported,
|
|
|
|
|
" we must redirect stderr (using shell temporarily)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(1, "decrypting file")
|
2012-05-30 22:18:01 -04:00
|
|
|
|
let cmd = { 'level': 1, 'ex': silent . 'r !' }
|
2015-09-29 19:48:18 -04:00
|
|
|
|
let cmd.args = '--quiet --decrypt ' . s:shellescape(filename, 1)
|
2011-11-23 15:30:35 -05:00
|
|
|
|
call s:GPGExecute(cmd)
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
if (v:shell_error) " message could not be decrypted
|
|
|
|
|
echohl GPGError
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let blackhole = input("Message could not be decrypted! (Press ENTER)")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2012-05-30 22:18:34 -04:00
|
|
|
|
" Only wipeout the buffer if we were creating one to start with.
|
|
|
|
|
" FileReadCmd just reads the content into the existing buffer
|
|
|
|
|
if a:bufread
|
|
|
|
|
silent bwipeout!
|
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2015-01-05 22:53:47 -05:00
|
|
|
|
if a:bufread
|
|
|
|
|
" In order to make :undo a no-op immediately after the buffer is read,
|
|
|
|
|
" we need to do this dance with 'undolevels'. Actually discarding the undo
|
|
|
|
|
" history requires performing a change after setting 'undolevels' to -1 and,
|
|
|
|
|
" luckily, we have one we need to do (delete the extra line from the :r
|
|
|
|
|
" command)
|
|
|
|
|
let levels = &undolevels
|
|
|
|
|
set undolevels=-1
|
|
|
|
|
" :lockmarks doesn't actually prevent '[,'] from being overwritten, so we
|
|
|
|
|
" need to manually set them ourselves instead
|
|
|
|
|
silent 1delete
|
|
|
|
|
1mark [
|
|
|
|
|
$mark ]
|
|
|
|
|
let &undolevels = levels
|
2015-11-04 21:02:31 -05:00
|
|
|
|
let &readonly = filereadable(filename) && filewritable(filename) == 0
|
2015-01-05 22:53:47 -05:00
|
|
|
|
" call the autocommand for the file minus .gpg$
|
2015-10-03 10:27:17 -04:00
|
|
|
|
silent execute ':doautocmd BufReadPost ' . autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called BufReadPost autocommand for ' . autocmd_filename)
|
2015-01-05 22:53:47 -05:00
|
|
|
|
else
|
|
|
|
|
" call the autocommand for the file minus .gpg$
|
2015-10-03 10:27:17 -04:00
|
|
|
|
silent execute ':doautocmd FileReadPost ' . autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called FileReadPost autocommand for ' . autocmd_filename)
|
2015-01-05 22:53:47 -05:00
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
" Allow the user to define actions for GnuPG buffers
|
|
|
|
|
silent doautocmd User GnuPG
|
|
|
|
|
|
2008-01-23 09:49:33 -05:00
|
|
|
|
" refresh screen
|
|
|
|
|
redraw!
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEncrypt() {{{2
|
|
|
|
|
"
|
|
|
|
|
" encrypts the buffer to all previous recipients
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGEncrypt()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()")
|
|
|
|
|
|
2014-12-25 15:11:31 -05:00
|
|
|
|
" FileWriteCmd is only called when a portion of a buffer is being written to
|
|
|
|
|
" disk. Since Vim always sets the '[,'] marks to the part of a buffer that
|
|
|
|
|
" is being written, that can be used to determine whether BufWriteCmd or
|
|
|
|
|
" FileWriteCmd triggered us.
|
|
|
|
|
if [line("'["), line("']")] == [1, line('$')]
|
|
|
|
|
let auType = 'BufWrite'
|
|
|
|
|
else
|
|
|
|
|
let auType = 'FileWrite'
|
|
|
|
|
endif
|
|
|
|
|
|
2015-10-03 10:27:17 -04:00
|
|
|
|
" file name minus extension
|
|
|
|
|
let autocmd_filename = fnameescape(expand('<afile>:r'))
|
|
|
|
|
|
|
|
|
|
silent exe ':doautocmd '. auType .'Pre '. autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called '. auType .'Pre autocommand for ' . autocmd_filename)
|
2014-12-25 15:11:31 -05:00
|
|
|
|
|
2008-01-23 09:49:33 -05:00
|
|
|
|
" store encoding and switch to a safe one
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (&fileencoding != &encoding)
|
2008-01-23 09:49:33 -05:00
|
|
|
|
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")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
2010-10-28 01:30:47 -04:00
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
echohl GPGError
|
2010-10-28 01:30:47 -04:00
|
|
|
|
let blackhole = input("Message could not be encrypted! (Press ENTER)")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" initialize GPGOptions if not happened before
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if (!exists("b:GPGOptions") || empty(b:GPGOptions))
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions = []
|
2007-03-01 09:58:04 -05:00
|
|
|
|
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["symmetric"]
|
|
|
|
|
let b:GPGRecipients = []
|
2007-03-01 09:58:04 -05:00
|
|
|
|
else
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["encrypt"]
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2007-03-01 09:58:04 -05:00
|
|
|
|
if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGOptions += ["armor"]
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2009-05-27 07:10:20 -04:00
|
|
|
|
if (exists("g:GPGPreferSign") && g:GPGPreferSign == 1)
|
|
|
|
|
let b:GPGOptions += ["sign"]
|
|
|
|
|
endif
|
2008-07-31 11:43:14 -04:00
|
|
|
|
call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions))
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
" built list of options
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let options = ""
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for option in b:GPGOptions
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let options = options . " --" . option . " "
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfor
|
|
|
|
|
|
2010-10-28 01:30:47 -04:00
|
|
|
|
if (!exists('b:GPGRecipients'))
|
|
|
|
|
let b:GPGRecipients = []
|
|
|
|
|
endif
|
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" check here again if all recipients are available in the keyring
|
2013-01-25 07:49:57 -05:00
|
|
|
|
let recipients = s:GPGCheckRecipients(b:GPGRecipients)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" check if there are unknown recipients and warn
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if !empty(recipients.unknown)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "Please use GPGEditRecipients to correct!!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echo
|
|
|
|
|
echohl None
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
|
|
|
|
" Let user know whats happend and copy known_recipients back to buffer
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let dummy = input("Press ENTER to quit")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" built list of recipients
|
2013-01-25 07:49:57 -05:00
|
|
|
|
let options .= ' ' . join(map(recipients.valid, '"-r ".v:val'), ' ')
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" encrypt the buffer
|
2010-10-28 01:30:47 -04:00
|
|
|
|
let destfile = tempname()
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let cmd = { 'level': 1, 'ex': "'[,']w !" }
|
|
|
|
|
let cmd.args = '--quiet --no-encrypt-to ' . options
|
2015-09-29 19:48:18 -04:00
|
|
|
|
let cmd.redirect = '>' . s:shellescape(destfile, 1)
|
2013-06-14 23:01:22 -04:00
|
|
|
|
silent call s:GPGExecute(cmd)
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
|
|
|
|
" restore encoding
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (s:GPGEncoding != "")
|
2008-01-23 09:49:33 -05:00
|
|
|
|
let &encoding = s:GPGEncoding
|
|
|
|
|
call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2010-10-28 01:30:47 -04:00
|
|
|
|
if (v:shell_error) " message could not be encrypted
|
|
|
|
|
" Command failed, so clean up the tempfile
|
|
|
|
|
call delete(destfile)
|
|
|
|
|
echohl GPGError
|
|
|
|
|
let blackhole = input("Message could not be encrypted! (Press ENTER)")
|
|
|
|
|
echohl None
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
|
|
|
|
|
return
|
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2015-11-04 22:03:35 -05:00
|
|
|
|
let filename = resolve(expand('<afile>'))
|
|
|
|
|
call rename(destfile, filename)
|
2014-12-25 15:11:52 -05:00
|
|
|
|
if auType == 'BufWrite'
|
|
|
|
|
setl nomodified
|
2015-11-04 22:03:35 -05:00
|
|
|
|
let &readonly = filereadable(filename) && filewritable(filename) == 0
|
2014-12-25 15:11:52 -05:00
|
|
|
|
endif
|
|
|
|
|
|
2015-10-03 10:27:17 -04:00
|
|
|
|
silent exe ':doautocmd '. auType .'Post '. autocmd_filename
|
|
|
|
|
call s:GPGDebug(2, 'called '. auType .'Post autocommand for ' . autocmd_filename)
|
2014-12-25 15:11:31 -05:00
|
|
|
|
|
2010-10-28 01:30:47 -04:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGViewRecipients() {{{2
|
|
|
|
|
"
|
|
|
|
|
" echo the recipients
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGViewRecipients()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewRecipients()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2013-01-25 07:49:57 -05:00
|
|
|
|
let recipients = s:GPGCheckRecipients(b:GPGRecipients)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
|
|
|
|
|
" echo the recipients
|
2013-01-25 07:49:57 -05:00
|
|
|
|
for name in recipients.valid
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let name = s:GPGIDToName(name)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echo name
|
|
|
|
|
endfor
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" echo the unknown recipients
|
|
|
|
|
echohl GPGWarning
|
2013-01-25 07:49:57 -05:00
|
|
|
|
for name in recipients.unknown
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let name = "!" . name
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echo name
|
|
|
|
|
endfor
|
|
|
|
|
echohl None
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" check if there is any known recipient
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if empty(recipients.valid)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echohl GPGError
|
|
|
|
|
echom 'There are no known recipients!'
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEditRecipients() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a scratch buffer with all recipients to add/remove recipients
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGEditRecipients()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditRecipients()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" only do this if it isn't already a GPGRecipients_* buffer
|
|
|
|
|
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
|
|
|
|
|
|
|
|
|
|
" save buffer name
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let buffername = bufname("%")
|
|
|
|
|
let editbuffername = "GPGRecipients_" . buffername
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" check if this buffer exists
|
|
|
|
|
if (!bufexists(editbuffername))
|
|
|
|
|
" create scratch buffer
|
2010-02-16 07:56:18 -05:00
|
|
|
|
execute 'silent! split ' . fnameescape(editbuffername)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" add a autocommand to regenerate the recipients after a write
|
2008-07-31 11:43:14 -04:00
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else
|
|
|
|
|
if (bufwinnr(editbuffername) >= 0)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" switch to scratch buffer window
|
|
|
|
|
execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" split scratch buffer window
|
2010-02-16 07:56:18 -05:00
|
|
|
|
execute 'silent! sbuffer ' . fnameescape(editbuffername)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" add a autocommand to regenerate the recipients after a write
|
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" empty the buffer
|
2010-10-28 01:30:47 -04:00
|
|
|
|
silent %delete
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" Mark the buffer as a scratch buffer
|
2008-07-31 11:43:14 -04:00
|
|
|
|
setlocal buftype=acwrite
|
|
|
|
|
setlocal bufhidden=hide
|
2006-12-15 13:09:40 -05:00
|
|
|
|
setlocal noswapfile
|
|
|
|
|
setlocal nowrap
|
|
|
|
|
setlocal nobuflisted
|
|
|
|
|
setlocal nonumber
|
|
|
|
|
|
|
|
|
|
" so we know for which other buffer this edit buffer is
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGCorrespondingTo = buffername
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" put some comments to the scratch buffer
|
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
2009-06-08 12:32:13 -04:00
|
|
|
|
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.'
|
|
|
|
|
silent put ='GPG: Data after recipients between and including \"(\" and \")\" is ignored.'
|
|
|
|
|
silent put ='GPG: Closing this buffer commits changes.'
|
2006-12-15 13:09:40 -05:00
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" get the recipients
|
2013-01-25 07:49:57 -05:00
|
|
|
|
let recipients = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients"))
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-08-15 12:50:33 -04:00
|
|
|
|
" if there are no known or unknown recipients, use the default ones
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if (empty(recipients.valid) && empty(recipients.unknown))
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (type(g:GPGDefaultRecipients) == type([]))
|
2013-01-25 07:49:57 -05:00
|
|
|
|
let recipients = s:GPGCheckRecipients(g:GPGDefaultRecipients)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
else
|
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!"
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" put the recipients in the scratch buffer
|
2013-01-25 07:49:57 -05:00
|
|
|
|
for name in recipients.valid
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let name = s:GPGIDToName(name)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
silent put =name
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfor
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" put the unknown recipients in the scratch buffer
|
2013-01-24 23:12:08 -05:00
|
|
|
|
let syntaxPattern = ''
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if !empty(recipients.unknown)
|
|
|
|
|
let flaggedNames = map(recipients.unknown, '"!".v:val')
|
2013-01-24 23:12:08 -05:00
|
|
|
|
call append('$', flaggedNames)
|
|
|
|
|
let syntaxPattern = '\(' . join(flaggedNames, '\|') . '\)'
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2014-10-29 11:53:59 -04:00
|
|
|
|
for line in g:GPGPossibleRecipients
|
|
|
|
|
silent put ='GPG: '.line
|
2014-10-09 10:48:03 -04:00
|
|
|
|
endfor
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" define highlight
|
|
|
|
|
if (has("syntax") && exists("g:syntax_on"))
|
|
|
|
|
highlight clear GPGUnknownRecipient
|
2013-01-24 23:12:08 -05:00
|
|
|
|
if !empty(syntaxPattern)
|
|
|
|
|
execute 'syntax match GPGUnknownRecipient "' . syntaxPattern . '"'
|
|
|
|
|
highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
syntax match GPGComment "^GPG:.*$"
|
2010-01-27 08:18:04 -05:00
|
|
|
|
execute 'syntax match GPGComment "' . s:GPGMagicString . '.*$"'
|
2006-12-15 13:09:40 -05:00
|
|
|
|
highlight clear GPGComment
|
|
|
|
|
highlight link GPGComment Comment
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" delete the empty first line
|
2010-10-28 01:30:47 -04:00
|
|
|
|
silent 1delete
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" jump to the first recipient
|
2010-10-28 01:30:47 -04:00
|
|
|
|
silent $
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGFinishRecipientsBuffer() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a new recipient list from RecipientsBuffer
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGFinishRecipientsBuffer()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishRecipientsBuffer()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" go to buffer before doing work
|
|
|
|
|
if (bufnr("%") != expand("<abuf>"))
|
|
|
|
|
" switch to scratch buffer window
|
2008-07-31 11:43:14 -04:00
|
|
|
|
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" delete the autocommand
|
|
|
|
|
autocmd! * <buffer>
|
|
|
|
|
|
|
|
|
|
" get the recipients from the scratch buffer
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let recipients = []
|
|
|
|
|
let lines = getline(1,"$")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for recipient in lines
|
2012-05-30 23:48:43 -04:00
|
|
|
|
let matches = matchlist(recipient, '^\(.\{-}\)\%(' . s:GPGMagicString . '(ID:\s\+\(' . s:keyPattern . '\)\s\+.*\)\=$')
|
|
|
|
|
|
|
|
|
|
let recipient = matches[2] ? matches[2] : matches[1]
|
2009-06-08 12:32:13 -04:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" delete all spaces at beginning and end of the recipient
|
|
|
|
|
" also delete a '!' at the beginning of the recipient
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
|
2009-06-08 12:32:13 -04:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" delete comment lines
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let recipient = substitute(recipient, "^GPG:.*$", "", "")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" only do this if the line is not empty
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if !empty(recipient)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let gpgid = s:GPGNameToID(recipient)
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if !empty(gpgid)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
if (match(recipients, gpgid) < 0)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let recipients += [gpgid]
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else
|
2008-07-31 11:43:14 -04:00
|
|
|
|
if (match(recipients, recipient) < 0)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let recipients += [recipient]
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
2008-08-15 12:50:33 -04:00
|
|
|
|
endif
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
|
|
|
|
endfor
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" write back the new recipient list to the corresponding buffer and mark it
|
2013-01-27 20:59:18 -05:00
|
|
|
|
" as modified. Buffer is now for sure an encrypted buffer.
|
2008-07-31 11:43:14 -04:00
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients)
|
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
|
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" check if there is any known recipient
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if empty(recipients)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl GPGError
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom 'There are no known recipients!'
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
" reset modified flag
|
2010-10-28 01:30:47 -04:00
|
|
|
|
setl nomodified
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" Function: s:GPGViewOptions() {{{2
|
|
|
|
|
"
|
|
|
|
|
" echo the recipients
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGViewOptions()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewOptions()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
if (exists("b:GPGOptions"))
|
|
|
|
|
echo 'This file has following options:'
|
|
|
|
|
" echo the options
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for option in b:GPGOptions
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echo option
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfor
|
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEditOptions() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a scratch buffer with all recipients to add/remove recipients
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGEditOptions()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditOptions()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" only do this if it isn't already a GPGOptions_* buffer
|
|
|
|
|
if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0)
|
|
|
|
|
|
|
|
|
|
" save buffer name
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let buffername = bufname("%")
|
|
|
|
|
let editbuffername = "GPGOptions_" . buffername
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" check if this buffer exists
|
|
|
|
|
if (!bufexists(editbuffername))
|
|
|
|
|
" create scratch buffer
|
2010-02-16 07:56:18 -05:00
|
|
|
|
execute 'silent! split ' . fnameescape(editbuffername)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" add a autocommand to regenerate the options after a write
|
2008-07-31 11:43:14 -04:00
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else
|
|
|
|
|
if (bufwinnr(editbuffername) >= 0)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" switch to scratch buffer window
|
|
|
|
|
execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" split scratch buffer window
|
2010-02-16 07:56:18 -05:00
|
|
|
|
execute 'silent! sbuffer ' . fnameescape(editbuffername)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" add a autocommand to regenerate the options after a write
|
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" empty the buffer
|
2010-10-28 01:30:47 -04:00
|
|
|
|
silent %delete
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" 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
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let b:GPGCorrespondingTo = buffername
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" 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!'
|
2009-06-08 12:32:13 -04:00
|
|
|
|
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.'
|
|
|
|
|
silent put ='GPG: Closing this buffer commits changes.'
|
2006-12-15 13:09:40 -05:00
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
|
|
|
|
|
|
" put the options in the scratch buffer
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for option in options
|
2006-12-15 13:09:40 -05:00
|
|
|
|
silent put =option
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfor
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" delete the empty first line
|
2010-10-28 01:30:47 -04:00
|
|
|
|
silent 1delete
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" jump to the first option
|
2010-10-28 01:30:47 -04:00
|
|
|
|
silent $
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" define highlight
|
|
|
|
|
if (has("syntax") && exists("g:syntax_on"))
|
|
|
|
|
syntax match GPGComment "^GPG:.*$"
|
|
|
|
|
highlight clear GPGComment
|
|
|
|
|
highlight link GPGComment Comment
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
|
|
|
|
endif
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGFinishOptionsBuffer() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a new option list from OptionsBuffer
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGFinishOptionsBuffer()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishOptionsBuffer()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-15 13:09:40 -05:00
|
|
|
|
echohl None
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
return
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" go to buffer before doing work
|
|
|
|
|
if (bufnr("%") != expand("<abuf>"))
|
|
|
|
|
" switch to scratch buffer window
|
2008-07-31 11:43:14 -04:00
|
|
|
|
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" clear options and unknownOptions
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let options = []
|
|
|
|
|
let unknownOptions = []
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" delete the autocommand
|
|
|
|
|
autocmd! * <buffer>
|
|
|
|
|
|
|
|
|
|
" get the options from the scratch buffer
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let lines = getline(1, "$")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for option in lines
|
|
|
|
|
" delete all spaces at beginning and end of the option
|
|
|
|
|
" also delete a '!' at the beginning of the option
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let option = substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" delete comment lines
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let option = substitute(option, "^GPG:.*$", "", "")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" only do this if the line is not empty
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if (!empty(option) && match(options, option) < 0)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let options += [option]
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
|
|
|
|
endfor
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" write back the new option list to the corresponding buffer and mark it
|
|
|
|
|
" as modified
|
2008-07-31 11:43:14 -04:00
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options)
|
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
|
|
|
|
|
|
|
|
|
|
" reset modified flag
|
2010-10-28 01:30:47 -04:00
|
|
|
|
setl nomodified
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
" Function: s:GPGCheckRecipients(tocheck) {{{2
|
|
|
|
|
"
|
|
|
|
|
" check if recipients are known
|
2013-01-25 07:49:57 -05:00
|
|
|
|
" Returns: dictionary of recipients, {'valid': [], 'unknown': []}
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGCheckRecipients(tocheck)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()")
|
|
|
|
|
|
2013-01-25 07:49:57 -05:00
|
|
|
|
let recipients = {'valid': [], 'unknown': []}
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
|
|
|
|
if (type(a:tocheck) == type([]))
|
|
|
|
|
for recipient in a:tocheck
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let gpgid = s:GPGNameToID(recipient)
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if !empty(gpgid)
|
|
|
|
|
if (match(recipients.valid, gpgid) < 0)
|
|
|
|
|
call add(recipients.valid, gpgid)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
|
|
|
|
else
|
2013-01-25 07:49:57 -05:00
|
|
|
|
if (match(recipients.unknown, recipient) < 0)
|
|
|
|
|
call add(recipients.unknown, recipient)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
|
|
|
|
end
|
|
|
|
|
endfor
|
|
|
|
|
endif
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
2013-01-25 07:49:57 -05:00
|
|
|
|
call s:GPGDebug(2, "recipients are: " . string(recipients.valid))
|
|
|
|
|
call s:GPGDebug(2, "unknown recipients are: " . string(recipients.unknown))
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()")
|
2013-01-25 07:49:57 -05:00
|
|
|
|
return recipients
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGNameToID(name) {{{2
|
|
|
|
|
"
|
|
|
|
|
" find GPG key ID corresponding to a name
|
|
|
|
|
" Returns: ID for the given name
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGNameToID(name)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" ask gpg for the id for a name
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let cmd = { 'level': 2 }
|
2015-09-29 19:48:18 -04:00
|
|
|
|
let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . s:shellescape(a:name)
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let output = s:GPGSystem(cmd)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
|
|
|
|
" so convert it, if necessary
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (&encoding != "utf-8")
|
|
|
|
|
let output = iconv(output, "utf-8", &encoding)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let lines = split(output, "\n")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" parse the output of gpg
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let pubseen = 0
|
|
|
|
|
let counter = 0
|
|
|
|
|
let gpgids = []
|
2013-01-27 20:59:18 -05:00
|
|
|
|
let seen_keys = {}
|
|
|
|
|
let skip_key = 0
|
|
|
|
|
let has_strftime = exists('*strftime')
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for line in lines
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2013-01-27 20:59:18 -05:00
|
|
|
|
let fields = split(line, ":")
|
2010-02-16 07:56:18 -05:00
|
|
|
|
|
2013-01-27 20:59:18 -05:00
|
|
|
|
" search for the next pub
|
|
|
|
|
if (fields[0] == "pub")
|
|
|
|
|
" check if this key has already been processed
|
|
|
|
|
if has_key(seen_keys, fields[4])
|
|
|
|
|
let skip_key = 1
|
|
|
|
|
continue
|
|
|
|
|
endif
|
|
|
|
|
let skip_key = 0
|
|
|
|
|
let seen_keys[fields[4]] = 1
|
2012-05-31 00:31:36 -04:00
|
|
|
|
|
2013-01-27 20:59:18 -05:00
|
|
|
|
" Ignore keys which are not usable for encryption
|
|
|
|
|
if fields[11] !~? 'e'
|
|
|
|
|
continue
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
let identity = fields[4]
|
|
|
|
|
let gpgids += [identity]
|
|
|
|
|
if has_strftime
|
|
|
|
|
let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n"
|
2012-05-31 00:31:36 -04:00
|
|
|
|
else
|
2013-01-27 20:59:18 -05:00
|
|
|
|
let choices = choices . counter . ": ID: 0x" . identity . "\n"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2013-01-27 20:59:18 -05:00
|
|
|
|
let counter = counter+1
|
|
|
|
|
let pubseen = 1
|
|
|
|
|
" search for the next uid
|
|
|
|
|
elseif (!skip_key && fields[0] == "uid")
|
|
|
|
|
let choices = choices . " " . fields[9] . "\n"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfor
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
|
|
|
|
" counter > 1 means we have more than one results
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let answer = 0
|
2006-12-15 13:09:40 -05:00
|
|
|
|
if (counter > 1)
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let choices = choices . "Enter number: "
|
|
|
|
|
let answer = input(choices, "0")
|
2006-12-15 13:09:40 -05:00
|
|
|
|
while (answer == "")
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let answer = input("Enter number: ", "0")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endwhile
|
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2010-02-16 07:56:18 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGNameToID()")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
return get(gpgids, answer, "")
|
|
|
|
|
endfunction
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGIDToName(identity) {{{2
|
|
|
|
|
"
|
|
|
|
|
" find name corresponding to a GPG key ID
|
|
|
|
|
" Returns: Name for the given ID
|
2010-01-27 08:18:04 -05:00
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGIDToName(identity)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGIDToName()")
|
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" TODO is the encryption subkey really unique?
|
|
|
|
|
|
|
|
|
|
" ask gpg for the id for a name
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let cmd = { 'level': 2 }
|
|
|
|
|
let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . a:identity
|
|
|
|
|
let output = s:GPGSystem(cmd)
|
2006-12-15 13:09:40 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
|
|
|
|
" so convert it, if necessary
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (&encoding != "utf-8")
|
|
|
|
|
let output = iconv(output, "utf-8", &encoding)
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let lines = split(output, "\n")
|
2008-07-31 11:43:14 -04:00
|
|
|
|
|
2006-12-15 13:09:40 -05:00
|
|
|
|
" parse the output of gpg
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let pubseen = 0
|
|
|
|
|
let uid = ""
|
2008-07-31 11:43:14 -04:00
|
|
|
|
for line in lines
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let fields = split(line, ":")
|
2012-05-31 00:31:36 -04:00
|
|
|
|
|
|
|
|
|
if !pubseen " search for the next pub
|
2008-07-31 11:43:14 -04:00
|
|
|
|
if (fields[0] == "pub")
|
2012-05-31 22:27:42 -04:00
|
|
|
|
" Ignore keys which are not usable for encryption
|
|
|
|
|
if fields[11] !~? 'e'
|
|
|
|
|
continue
|
|
|
|
|
endif
|
|
|
|
|
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let pubseen = 1
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
2006-12-15 13:09:40 -05:00
|
|
|
|
else " search for the next uid
|
2008-07-31 11:43:14 -04:00
|
|
|
|
if (fields[0] == "uid")
|
2008-08-15 12:50:33 -04:00
|
|
|
|
let pubseen = 0
|
2009-06-08 12:32:13 -04:00
|
|
|
|
if exists("*strftime")
|
|
|
|
|
let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . " created at " . strftime("%c", fields[5]) . ")"
|
|
|
|
|
else
|
|
|
|
|
let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . ")"
|
|
|
|
|
endif
|
2008-07-31 11:43:14 -04:00
|
|
|
|
break
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
endfor
|
2003-05-30 09:29:16 -04:00
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
|
2003-05-30 09:29:16 -04:00
|
|
|
|
return uid
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endfunction
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2015-09-29 19:23:19 -04:00
|
|
|
|
" Function: s:GPGPreCmd() {{{2
|
|
|
|
|
"
|
|
|
|
|
" Setup the environment for running the gpg command
|
|
|
|
|
"
|
2011-11-23 15:30:35 -05:00
|
|
|
|
function s:GPGPreCmd()
|
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
|
|
|
|
let &shelltemp = s:shelltemp
|
2015-09-29 19:23:19 -04:00
|
|
|
|
" Force C locale so GPG output is consistent
|
|
|
|
|
let s:messages = v:lang
|
|
|
|
|
language messages C
|
2011-11-23 15:30:35 -05:00
|
|
|
|
endfunction
|
|
|
|
|
|
2015-09-29 19:23:19 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGPostCmd() {{{2
|
|
|
|
|
"
|
|
|
|
|
" Restore the user's environment after running the gpg command
|
|
|
|
|
"
|
2011-11-23 15:30:35 -05:00
|
|
|
|
function s:GPGPostCmd()
|
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
let &shell = s:shellsave
|
|
|
|
|
let &shelltemp = s:shelltempsave
|
2015-09-29 19:23:19 -04:00
|
|
|
|
execute 'language messages' s:messages
|
2014-11-20 23:18:59 -05:00
|
|
|
|
" Workaround a bug in the interaction between console vim and
|
|
|
|
|
" pinentry-curses by forcing Vim to re-detect and setup its terminal
|
|
|
|
|
" settings
|
|
|
|
|
let &term = &term
|
2015-03-21 21:46:00 -04:00
|
|
|
|
silent doautocmd TermChanged
|
2011-11-23 15:30:35 -05:00
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
" Function: s:GPGSystem(dict) {{{2
|
|
|
|
|
"
|
2015-03-02 23:18:13 -05:00
|
|
|
|
" run g:GPGCommand using system(), logging the commandline and output. This
|
|
|
|
|
" uses temp files (regardless of how 'shelltemp' is set) to hold the output of
|
|
|
|
|
" the command, so it must not be used for sensitive commands.
|
2011-11-23 15:30:35 -05:00
|
|
|
|
" Recognized keys are:
|
|
|
|
|
" level - Debug level at which the commandline and output will be logged
|
|
|
|
|
" args - Arguments to be given to g:GPGCommand
|
|
|
|
|
"
|
|
|
|
|
" Returns: command output
|
|
|
|
|
"
|
|
|
|
|
function s:GPGSystem(dict)
|
2014-08-10 18:48:00 -04:00
|
|
|
|
let commandline = s:GPGCommand
|
2011-11-23 15:35:01 -05:00
|
|
|
|
if (!empty(g:GPGHomedir))
|
2015-09-29 19:48:18 -04:00
|
|
|
|
let commandline .= ' --homedir ' . s:shellescape(g:GPGHomedir)
|
2011-11-23 15:35:01 -05:00
|
|
|
|
endif
|
2014-08-10 18:48:00 -04:00
|
|
|
|
let commandline .= ' ' . a:dict.args
|
2011-11-23 15:30:35 -05:00
|
|
|
|
let commandline .= ' ' . s:stderrredirnull
|
|
|
|
|
call s:GPGDebug(a:dict.level, "command: ". commandline)
|
|
|
|
|
|
|
|
|
|
call s:GPGPreCmd()
|
|
|
|
|
let output = system(commandline)
|
|
|
|
|
call s:GPGPostCmd()
|
|
|
|
|
|
2012-12-21 21:16:38 -05:00
|
|
|
|
call s:GPGDebug(a:dict.level, "rc: ". v:shell_error)
|
2011-11-23 15:30:35 -05:00
|
|
|
|
call s:GPGDebug(a:dict.level, "output: ". output)
|
|
|
|
|
return output
|
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
|
" Function: s:GPGExecute(dict) {{{2
|
|
|
|
|
"
|
|
|
|
|
" run g:GPGCommand using :execute, logging the commandline
|
|
|
|
|
" Recognized keys are:
|
|
|
|
|
" level - Debug level at which the commandline will be logged
|
|
|
|
|
" args - Arguments to be given to g:GPGCommand
|
|
|
|
|
" ex - Ex command which will be :executed
|
|
|
|
|
" redirect - Shell redirect to use, if needed
|
|
|
|
|
"
|
|
|
|
|
function s:GPGExecute(dict)
|
2014-08-10 18:48:00 -04:00
|
|
|
|
let commandline = printf('%s%s', a:dict.ex, s:GPGCommand)
|
2011-11-23 15:35:01 -05:00
|
|
|
|
if (!empty(g:GPGHomedir))
|
2015-09-29 19:48:18 -04:00
|
|
|
|
let commandline .= ' --homedir ' . s:shellescape(g:GPGHomedir, 1)
|
2011-11-23 15:35:01 -05:00
|
|
|
|
endif
|
2014-08-10 18:48:00 -04:00
|
|
|
|
let commandline .= ' ' . a:dict.args
|
2011-11-23 15:30:35 -05:00
|
|
|
|
if (has_key(a:dict, 'redirect'))
|
|
|
|
|
let commandline .= ' ' . a:dict.redirect
|
|
|
|
|
endif
|
|
|
|
|
let commandline .= ' ' . s:stderrredirnull
|
|
|
|
|
call s:GPGDebug(a:dict.level, "command: " . commandline)
|
|
|
|
|
|
|
|
|
|
call s:GPGPreCmd()
|
|
|
|
|
execute commandline
|
|
|
|
|
call s:GPGPostCmd()
|
2012-12-21 21:16:38 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(a:dict.level, "rc: ". v:shell_error)
|
2011-11-23 15:30:35 -05:00
|
|
|
|
endfunction
|
|
|
|
|
|
2008-01-23 09:49:33 -05:00
|
|
|
|
" Function: s:GPGDebug(level, text) {{{2
|
|
|
|
|
"
|
|
|
|
|
" output debug message, if this message has high enough importance
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" only define function if GPGDebugLevel set at all
|
|
|
|
|
"
|
2008-07-31 11:43:14 -04:00
|
|
|
|
function s:GPGDebug(level, text)
|
2010-01-27 08:18:04 -05:00
|
|
|
|
if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level
|
|
|
|
|
if exists("g:GPGDebugLog")
|
|
|
|
|
execute "redir >> " . g:GPGDebugLog
|
2012-05-31 21:21:35 -04:00
|
|
|
|
silent echom "GnuPG: " . a:text
|
2010-01-27 08:18:04 -05:00
|
|
|
|
redir END
|
|
|
|
|
else
|
|
|
|
|
echom "GnuPG: " . a:text
|
|
|
|
|
endif
|
2008-07-31 11:43:14 -04:00
|
|
|
|
endif
|
|
|
|
|
endfunction
|
2008-01-23 09:49:33 -05:00
|
|
|
|
|
2010-01-27 08:18:04 -05:00
|
|
|
|
" Section: Commands {{{1
|
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
command! GPGViewRecipients call s:GPGViewRecipients()
|
|
|
|
|
command! GPGEditRecipients call s:GPGEditRecipients()
|
|
|
|
|
command! GPGViewOptions call s:GPGViewOptions()
|
|
|
|
|
command! GPGEditOptions call s:GPGEditOptions()
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2008-07-31 11:43:14 -04:00
|
|
|
|
" Section: Menu {{{1
|
2010-01-27 08:18:04 -05:00
|
|
|
|
|
2008-08-15 12:50:33 -04:00
|
|
|
|
if (has("menu"))
|
2008-07-31 11:43:14 -04:00
|
|
|
|
amenu <silent> Plugin.GnuPG.View\ Recipients :GPGViewRecipients<CR>
|
|
|
|
|
amenu <silent> Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients<CR>
|
|
|
|
|
amenu <silent> Plugin.GnuPG.View\ Options :GPGViewOptions<CR>
|
|
|
|
|
amenu <silent> Plugin.GnuPG.Edit\ Options :GPGEditOptions<CR>
|
|
|
|
|
endif
|
2009-05-27 07:10:20 -04:00
|
|
|
|
|
|
|
|
|
" vim600: set foldmethod=marker foldlevel=0 :
|