2009-07-23 03:02:52 -04:00
|
|
|
|
" Name: gnupg.vim
|
|
|
|
|
" 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
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
|
|
|
|
" Description: {{{2
|
2009-04-29 03:34:40 -04:00
|
|
|
|
"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" 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
|
|
|
|
"
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" Installation: {{{2
|
2002-04-24 04:59:12 -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 07: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:
|
|
|
|
|
"
|
2008-10-05 16:41:52 -04:00
|
|
|
|
" GPG_TTY=`tty`
|
2008-07-31 07: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.
|
|
|
|
|
" ...
|
|
|
|
|
"
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" Commands: {{{2
|
2002-04-24 04:59:12 -04:00
|
|
|
|
"
|
|
|
|
|
" :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.
|
|
|
|
|
"
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" Variables: {{{2
|
2006-12-08 11:02:33 -05:00
|
|
|
|
"
|
2011-07-05 02:57:30 -04:00
|
|
|
|
" g:GPGExecutable
|
|
|
|
|
" If set used as gpg executable, otherwise the system chooses what is run
|
|
|
|
|
" when "gpg" is called. Defaults to "gpg".
|
|
|
|
|
"
|
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.
|
|
|
|
|
"
|
2009-05-11 04:47:47 -04:00
|
|
|
|
" g:GPGPreferSign
|
|
|
|
|
" If set to 1 signed data is preferred for new files. Defaults to 0.
|
|
|
|
|
"
|
2008-08-04 02:27:59 -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.
|
|
|
|
|
"
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" Known Issues: {{{2
|
2009-04-29 03:34:40 -04:00
|
|
|
|
"
|
2009-05-27 03:10:20 -04:00
|
|
|
|
" In some cases gvim can't decryt files
|
2009-04-29 03:34:40 -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.
|
|
|
|
|
"
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" Credits: {{{2
|
2009-05-27 02:54:33 -04:00
|
|
|
|
"
|
2009-04-29 03:34:40 -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.
|
|
|
|
|
" - Thomas Arendsen Hein for patch to convert encoding of gpg output
|
|
|
|
|
" - 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.
|
2009-05-11 04:47:47 -04:00
|
|
|
|
" - Tim Swast for patch to generate signed files
|
2006-12-12 17:46:03 -05:00
|
|
|
|
"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" Section: Plugin header {{{1
|
2008-07-28 17:01:41 -04:00
|
|
|
|
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" guard against multiple loads {{{2
|
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
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2007-04-05 06:50:38 -04:00
|
|
|
|
let g:loaded_gnupg = "$Revision$"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" check for correct vim version {{{2
|
|
|
|
|
if (v:version < 700)
|
|
|
|
|
echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.0' | echohl None
|
|
|
|
|
finish
|
|
|
|
|
endif
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" Section: Autocmd setup {{{1
|
2009-07-23 03:02:52 -04:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
augroup GnuPG
|
2008-01-28 04:30:59 -05:00
|
|
|
|
autocmd!
|
|
|
|
|
|
|
|
|
|
" initialize the internal variables
|
|
|
|
|
autocmd BufNewFile,BufReadPre,FileReadPre *.\(gpg\|asc\|pgp\) call s:GPGInit()
|
|
|
|
|
" force the user to edit the recipient list if he opens a new file and public
|
|
|
|
|
" keys are preferred
|
2008-07-28 17:46:53 -04:00
|
|
|
|
autocmd BufNewFile *.\(gpg\|asc\|pgp\) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 0) | call s:GPGEditRecipients() | endif
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" do the decryption
|
|
|
|
|
autocmd BufReadPost,FileReadPost *.\(gpg\|asc\|pgp\) call s:GPGDecrypt()
|
|
|
|
|
|
|
|
|
|
" convert all text to encrypted text before writing
|
|
|
|
|
autocmd BufWritePre,FileWritePre *.\(gpg\|asc\|pgp\) call s:GPGEncrypt()
|
|
|
|
|
" undo the encryption so we are back in the normal text, directly
|
|
|
|
|
" after the file has been written.
|
|
|
|
|
autocmd BufWritePost,FileWritePost *.\(gpg\|asc\|pgp\) call s:GPGEncryptPost()
|
2008-08-15 05:53:06 -04:00
|
|
|
|
|
|
|
|
|
" cleanup on leaving vim
|
|
|
|
|
autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup()
|
2002-04-24 04:59:12 -04:00
|
|
|
|
augroup END
|
2011-07-05 02:56:18 -04:00
|
|
|
|
|
2009-06-08 08:32:13 -04:00
|
|
|
|
" Section: Constants {{{1
|
2009-07-23 03:02:52 -04:00
|
|
|
|
|
2009-06-08 08:32:13 -04:00
|
|
|
|
let s:GPGMagicString = "\t \t"
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" Section: Highlight setup {{{1
|
2009-07-23 03:02:52 -04:00
|
|
|
|
|
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
|
2009-07-23 03:02:52 -04:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" Function: s:GPGInit() {{{2
|
|
|
|
|
"
|
|
|
|
|
" initialize the plugin
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGInit()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGInit()")
|
|
|
|
|
|
2011-07-05 02:56:18 -04:00
|
|
|
|
" first make sure nothing is written to ~/.viminfo while editing
|
|
|
|
|
" an encrypted file.
|
2009-04-29 03:34:40 -04:00
|
|
|
|
set viminfo=
|
2011-07-05 02:56:18 -04:00
|
|
|
|
|
|
|
|
|
" we don't want a swap file, as it writes unencrypted data to disk
|
|
|
|
|
set noswapfile
|
|
|
|
|
|
2011-07-05 02:57:30 -04:00
|
|
|
|
" check what gpg command to use
|
|
|
|
|
if (!exists("g:GPGExecutable"))
|
2008-05-12 05:07:42 -04:00
|
|
|
|
let g:GPGExecutable = "gpg --trust-model always"
|
2011-07-05 02:57:30 -04:00
|
|
|
|
endif
|
|
|
|
|
|
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
|
|
|
|
|
|
2009-05-11 04:47:47 -04:00
|
|
|
|
" check if signed files are preferred
|
|
|
|
|
if (!exists("g:GPGPreferSign"))
|
|
|
|
|
let g:GPGPreferSign = 0
|
|
|
|
|
endif
|
|
|
|
|
|
2009-07-17 05:02:15 -04:00
|
|
|
|
" start with empty default recipients if none is defined so far
|
2008-08-05 06:51:48 -04:00
|
|
|
|
if (!exists("g:GPGDefaultRecipients"))
|
|
|
|
|
let g:GPGDefaultRecipients = []
|
|
|
|
|
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)
|
2008-05-12 05:18:49 -04:00
|
|
|
|
if (!exists("$GPG_TTY") && !has("gui_running"))
|
|
|
|
|
let $GPG_TTY = system("tty")
|
|
|
|
|
if (v:shell_error)
|
|
|
|
|
let $GPG_TTY = ""
|
|
|
|
|
echohl GPGError
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "The GPG_TTY is not set and no TTY could be found using the `tty` command!"
|
|
|
|
|
echom "gpg-agent might not work."
|
2008-05-12 05:18:49 -04:00
|
|
|
|
echohl None
|
|
|
|
|
endif
|
2006-11-16 11:12:57 -05:00
|
|
|
|
endif
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let s:GPGCommand = g:GPGExecutable . " --use-agent"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
else
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let s:GPGCommand = g:GPGExecutable . " --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
|
2008-08-15 05:34:34 -04:00
|
|
|
|
if (has("gui_running"))
|
|
|
|
|
let s:GPGCommand = s:GPGCommand . " --no-tty"
|
2007-03-01 04:58:04 -05:00
|
|
|
|
endif
|
|
|
|
|
|
2006-12-08 05:27:58 -05:00
|
|
|
|
" setup shell environment for unix and windows
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let s:shellredirsave = &shellredir
|
|
|
|
|
let s:shellsave = &shell
|
2008-10-15 07:43:54 -04:00
|
|
|
|
if (has("unix"))
|
2006-12-08 05:27:58 -05:00
|
|
|
|
" unix specific settings
|
|
|
|
|
let s:shellredir = &shellredir
|
2011-07-05 06:49:45 -04:00
|
|
|
|
let s:shell = '/bin/sh'
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let s:stderrredirnull = '2>/dev/null'
|
|
|
|
|
let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand
|
2008-12-15 08:16:44 -05:00
|
|
|
|
else
|
|
|
|
|
" windows specific settings
|
|
|
|
|
let s:shellredir = '>%s'
|
|
|
|
|
let s:shell = &shell
|
|
|
|
|
let s:stderrredirnull = '2>nul'
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-08 05:27:58 -05:00
|
|
|
|
|
2011-07-05 06:51:12 -04:00
|
|
|
|
call s:GPGDebug(3, "shellredirsave: " . s:shellredirsave)
|
|
|
|
|
call s:GPGDebug(3, "shellsave: " . s:shellsave)
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" find the supported algorithms
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let commandline = s:GPGCommand . " --version"
|
|
|
|
|
call s:GPGDebug(2, "command: ". commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let output = system(commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
let &shell = s:shellsave
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(2, "output: ". output)
|
2008-08-15 05:34:34 -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-19 04:58:57 -05:00
|
|
|
|
let s:GPGCompress = substitute(output, ".*Compress.\\{-}: \\(.\\{-}\\)\n.*", "\\1", "")
|
|
|
|
|
|
|
|
|
|
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()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-08-15 05:53:06 -04:00
|
|
|
|
" Function: s:GPGCleanup() {{{2
|
|
|
|
|
"
|
|
|
|
|
" cleanup on leaving vim
|
|
|
|
|
"
|
|
|
|
|
function s:GPGCleanup()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCleanup()")
|
|
|
|
|
|
2008-08-15 05:53:06 -04:00
|
|
|
|
" wipe out screen
|
|
|
|
|
new +only
|
|
|
|
|
redraw!
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCleanup()")
|
2008-08-15 05:53:06 -04:00
|
|
|
|
endfunction
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" Function: s:GPGDecrypt() {{{2
|
|
|
|
|
"
|
|
|
|
|
" decrypt the buffer and find all recipients of the encrypted file
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGDecrypt()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering 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
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let filename = escape(expand("%:p"), '\"')
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-30 10:48:53 -04:00
|
|
|
|
" clear GPGEncrypted, GPGRecipients and GPGOptions
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGEncrypted = 0
|
|
|
|
|
let b:GPGRecipients = []
|
|
|
|
|
let b:GPGOptions = []
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" find the recipients of the file
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let commandline = s:GPGCommand . " --verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 \"" . filename . "\""
|
|
|
|
|
call s:GPGDebug(3, "command: " . commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let output = system(commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
let &shell = s:shellsave
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "output: ". 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
|
2008-08-15 05:34:34 -04: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
|
|
|
|
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions += ["symmetric"]
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2011-07-05 06:46:39 -04:00
|
|
|
|
" find the used cipher algorithm
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let cipher = substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let 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
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "The cipher " . cipher . " is not known by the local gpg command. Using default!"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echo
|
|
|
|
|
echohl None
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
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
|
2008-08-15 05:34:34 -04: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
|
|
|
|
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions += ["encrypt"]
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2011-07-05 06:46:39 -04:00
|
|
|
|
" find the used public keys
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
while (start >= 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let start = start + strlen("gpg: public key is ")
|
|
|
|
|
let recipient = strpart(output, start, 8)
|
2011-07-05 02:51:02 -04:00
|
|
|
|
call s:GPGDebug(1, "recipient is " . recipient)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let name = s:GPGNameToID(recipient)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
if (strlen(name) > 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let 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
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGRecipients += [recipient]
|
2008-01-28 04:30:59 -05:00
|
|
|
|
echohl GPGWarning
|
2011-07-05 06:46:39 -04:00
|
|
|
|
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
|
2008-01-28 04:30:59 -05:00
|
|
|
|
echohl None
|
2002-04-24 04:59:12 -04:00
|
|
|
|
end
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let start = match(output, "gpg: public key is [[:xdigit:]]\\{8}", start)
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endwhile
|
2007-12-11 04:43:38 -05:00
|
|
|
|
else
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" file is not encrypted
|
2008-08-15 05:34:34 -04:00
|
|
|
|
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
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2011-07-05 02:56:18 -04:00
|
|
|
|
set nobin
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" 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")
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions += ["armor"]
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" 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)
|
2009-11-20 15:39:49 -05:00
|
|
|
|
call s:GPGDebug(1, "decrypting file")
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let commandline = "'[,']!" . s:GPGCommand . " --quiet --decrypt " . s:stderrredirnull
|
|
|
|
|
call s:GPGDebug(1, "command: " . commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
2011-07-05 06:51:12 -04:00
|
|
|
|
execute commandline
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
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
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let blackhole = input("Message could not be decrypted! (Press ENTER)")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl None
|
|
|
|
|
bwipeout
|
2011-07-05 02:56:18 -04:00
|
|
|
|
set nobin
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2011-07-05 02:56:18 -04:00
|
|
|
|
|
|
|
|
|
" turn off binary mode
|
|
|
|
|
set nobin
|
|
|
|
|
|
|
|
|
|
" call the autocommand for the file minus .gpg$
|
2008-07-30 07:55:47 -04:00
|
|
|
|
execute ":doautocmd BufReadPost " . escape(expand("%:r"), ' *?\"'."'")
|
|
|
|
|
call s:GPGDebug(2, "called autocommand for " . escape(expand("%:r"), ' *?\"'."'"))
|
2011-07-05 02:56:18 -04:00
|
|
|
|
|
|
|
|
|
" refresh screen
|
|
|
|
|
redraw!
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEncrypt() {{{2
|
|
|
|
|
"
|
|
|
|
|
" encrypts the buffer to all previous recipients
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGEncrypt()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()")
|
|
|
|
|
|
2008-01-23 04:49:33 -05:00
|
|
|
|
" save window view
|
2008-07-28 17:01:41 -04:00
|
|
|
|
let s:GPGWindowView = winsaveview()
|
|
|
|
|
call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView))
|
2011-07-05 02:55:50 -04:00
|
|
|
|
|
2008-01-23 03:58:02 -05:00
|
|
|
|
" store encoding and switch to a safe one
|
2008-08-15 05:34:34 -04:00
|
|
|
|
if (&fileencoding != &encoding)
|
2008-01-23 03:58:02 -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-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-01-23 03:58:02 -05:00
|
|
|
|
|
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
|
2009-09-24 03:18:49 -04:00
|
|
|
|
if (!exists("b:GPGEncrypted") || b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGError
|
|
|
|
|
let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
2011-07-05 06:46:39 -04:00
|
|
|
|
" initialize GPGOptions if not happened before
|
2008-07-28 17:01:41 -04:00
|
|
|
|
if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions = []
|
2006-12-17 16:20:33 -05:00
|
|
|
|
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions += ["symmetric"]
|
|
|
|
|
let b:GPGRecipients = []
|
2006-12-17 16:20:33 -05:00
|
|
|
|
else
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions += ["encrypt"]
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-17 16:20:33 -05:00
|
|
|
|
if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGOptions += ["armor"]
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2009-05-11 04:47:47 -04:00
|
|
|
|
if (exists("g:GPGPreferSign") && g:GPGPreferSign == 1)
|
|
|
|
|
let b:GPGOptions += ["sign"]
|
|
|
|
|
endif
|
2008-07-28 17:01:41 -04:00
|
|
|
|
call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions))
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2011-07-05 06:46:39 -04:00
|
|
|
|
|
|
|
|
|
" built list of options
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let options = ""
|
2008-07-28 17:01:41 -04:00
|
|
|
|
for option in b:GPGOptions
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let options = options . " --" . option . " "
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-30 10:15:02 -04:00
|
|
|
|
" check here again if all recipients are available in the keyring
|
2008-07-30 10:48:53 -04:00
|
|
|
|
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients)
|
2008-05-12 05:07:42 -04:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" check if there are unknown recipients and warn
|
2008-08-15 05:16:44 -04:00
|
|
|
|
if (len(unknownrecipients) > 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "Please use GPGEditRecipients to correct!!"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echo
|
|
|
|
|
echohl None
|
2008-07-28 17:01:41 -04:00
|
|
|
|
|
|
|
|
|
" Let user know whats happend and copy known_recipients back to buffer
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let dummy = input("Press ENTER to quit")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" built list of recipients
|
2008-07-30 10:48:53 -04:00
|
|
|
|
if (len(recipients) > 0)
|
|
|
|
|
for gpgid in recipients
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let options = options . " -r " . gpgid
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
else
|
2011-07-05 03:35:11 -04:00
|
|
|
|
if (match(b:GPGOptions, "encrypt") >= 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl GPGError
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "There are no recipients!!"
|
|
|
|
|
echom "Please use GPGEditRecipients to correct!!"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echo
|
|
|
|
|
echohl None
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" encrypt the buffer
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let commandline = "'[,']!" . s:GPGCommand . " --quiet --no-encrypt-to " . options . " " . s:stderrredirnull
|
|
|
|
|
call s:GPGDebug(1, "command: " . commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
2011-07-05 06:51:12 -04:00
|
|
|
|
silent execute commandline
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
let &shell = s:shellsave
|
2002-04-24 04:59:12 -04:00
|
|
|
|
if (v:shell_error) " message could not be encrypted
|
2008-08-15 08:50:33 -04:00
|
|
|
|
" delete content of the buffer to be sure no data is written unencrypted
|
|
|
|
|
" content will be recovered in GPGEncryptPost()
|
|
|
|
|
silent normal! 1GdG
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl GPGError
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let blackhole = input("Message could not be encrypted! File might be empty! (Press ENTER)")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2011-07-05 02:55:50 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEncryptPost() {{{2
|
|
|
|
|
"
|
|
|
|
|
" undo changes don by encrypt, after writing
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGEncryptPost()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncryptPost()")
|
|
|
|
|
|
2011-07-05 06:46:39 -04:00
|
|
|
|
" guard for unencrypted files
|
2011-07-05 02:55:50 -04:00
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()")
|
2011-07-05 02:55:50 -04:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2011-07-05 02:55:50 -04:00
|
|
|
|
|
|
|
|
|
" undo encryption of buffer content
|
|
|
|
|
silent u
|
|
|
|
|
|
|
|
|
|
" switch back from binary mode
|
|
|
|
|
set nobin
|
|
|
|
|
|
2008-01-23 03:58:02 -05:00
|
|
|
|
" restore encoding
|
2008-08-15 05:34:34 -04:00
|
|
|
|
if (s:GPGEncoding != "")
|
2008-01-23 03:58:02 -05:00
|
|
|
|
let &encoding = s:GPGEncoding
|
|
|
|
|
call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-01-23 03:58:02 -05:00
|
|
|
|
|
2008-01-23 04:49:33 -05:00
|
|
|
|
" restore window view
|
2008-07-28 17:01:41 -04:00
|
|
|
|
call winrestview(s:GPGWindowView)
|
|
|
|
|
call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView))
|
2011-07-05 02:55:50 -04:00
|
|
|
|
|
|
|
|
|
" refresh screen
|
2011-07-05 02:51:02 -04:00
|
|
|
|
redraw!
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncryptPost()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGViewRecipients() {{{2
|
|
|
|
|
"
|
|
|
|
|
" echo the recipients
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGViewRecipients()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewRecipients()")
|
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
2008-07-30 10:48:53 -04:00
|
|
|
|
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-30 10:48:53 -04:00
|
|
|
|
echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
|
|
|
|
|
" echo the recipients
|
|
|
|
|
for name in recipients
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let name = s:GPGIDToName(name)
|
2008-07-30 10:48:53 -04:00
|
|
|
|
echo name
|
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-30 10:48:53 -04:00
|
|
|
|
" echo the unknown recipients
|
|
|
|
|
echohl GPGWarning
|
|
|
|
|
for name in unknownrecipients
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let name = "!" . name
|
2008-07-30 10:48:53 -04:00
|
|
|
|
echo name
|
|
|
|
|
endfor
|
|
|
|
|
echohl None
|
|
|
|
|
|
|
|
|
|
" check if there is any known recipient
|
|
|
|
|
if (len(recipients) == 0)
|
|
|
|
|
echohl GPGError
|
|
|
|
|
echom 'There are no known recipients!'
|
|
|
|
|
echohl None
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEditRecipients() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a scratch buffer with all recipients to add/remove recipients
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGEditRecipients()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditRecipients()")
|
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
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
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let buffername = bufname("%")
|
|
|
|
|
let editbuffername = "GPGRecipients_" . buffername
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" check if this buffer exists
|
2006-12-14 05:03:19 -05:00
|
|
|
|
if (!bufexists(editbuffername))
|
|
|
|
|
" create scratch buffer
|
2008-07-30 07:55:47 -04:00
|
|
|
|
execute 'silent! split ' . escape(editbuffername, ' *?\"'."'")
|
2006-12-14 05:03:19 -05:00
|
|
|
|
|
|
|
|
|
" add a autocommand to regenerate the recipients after a write
|
2008-01-28 04:21:04 -05:00
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
|
2006-12-14 05:03:19 -05:00
|
|
|
|
else
|
|
|
|
|
if (bufwinnr(editbuffername) >= 0)
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" switch to scratch buffer window
|
2011-07-05 06:45:44 -04:00
|
|
|
|
execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
|
2006-12-14 05:03:19 -05:00
|
|
|
|
else
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" split scratch buffer window
|
2008-07-30 07:55:47 -04:00
|
|
|
|
execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'")
|
2006-12-14 05:51:22 -05:00
|
|
|
|
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" add a autocommand to regenerate the recipients after a write
|
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishRecipientsBuffer()
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-14 05:03:19 -05:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" empty the buffer
|
|
|
|
|
silent normal! 1GdG
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Mark the buffer as a scratch buffer
|
2008-08-15 08:50:33 -04:00
|
|
|
|
setlocal buftype=acwrite
|
|
|
|
|
setlocal bufhidden=hide
|
2002-04-24 04:59:12 -04:00
|
|
|
|
setlocal noswapfile
|
|
|
|
|
setlocal nowrap
|
|
|
|
|
setlocal nobuflisted
|
|
|
|
|
setlocal nonumber
|
|
|
|
|
|
|
|
|
|
" so we know for which other buffer this edit buffer is
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGCorrespondingTo = buffername
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" put some comments to the scratch buffer
|
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
2009-06-08 08: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.'
|
2002-04-24 04:59:12 -04:00
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
|
|
2008-07-30 10:48:53 -04:00
|
|
|
|
" get the recipients
|
|
|
|
|
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients"))
|
|
|
|
|
|
2008-08-04 02:27:59 -04:00
|
|
|
|
" if there are no known or unknown recipients, use the default ones
|
2008-08-05 06:51:48 -04:00
|
|
|
|
if (len(recipients) == 0 && len(unknownrecipients) == 0)
|
2008-08-04 02:27:59 -04:00
|
|
|
|
if (type(g:GPGDefaultRecipients) == type([]))
|
|
|
|
|
let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients)
|
|
|
|
|
else
|
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!"
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
|
|
|
|
endif
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" put the recipients in the scratch buffer
|
2008-07-28 17:01:41 -04:00
|
|
|
|
for name in recipients
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let name = s:GPGIDToName(name)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
silent put =name
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" put the unknown recipients in the scratch buffer
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let syntaxPattern = "\\(nonexxistinwordinthisbuffer"
|
2011-07-05 06:46:39 -04:00
|
|
|
|
for name in unknownrecipients
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let name = "!" . name
|
|
|
|
|
let syntaxPattern = syntaxPattern . "\\|" . name
|
2002-04-24 04:59:12 -04:00
|
|
|
|
silent put =name
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let syntaxPattern = syntaxPattern . "\\)"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" define highlight
|
|
|
|
|
if (has("syntax") && exists("g:syntax_on"))
|
2011-07-05 06:51:12 -04:00
|
|
|
|
execute 'syntax match GPGUnknownRecipient "' . syntaxPattern . '"'
|
2002-04-24 04:59:12 -04:00
|
|
|
|
highlight clear GPGUnknownRecipient
|
|
|
|
|
highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient
|
|
|
|
|
|
|
|
|
|
syntax match GPGComment "^GPG:.*$"
|
2011-07-05 06:51:12 -04:00
|
|
|
|
execute 'syntax match GPGComment "' . s:GPGMagicString . '.*$"'
|
2002-04-24 04:59:12 -04:00
|
|
|
|
highlight clear GPGComment
|
|
|
|
|
highlight link GPGComment Comment
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" 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
|
|
|
|
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGFinishRecipientsBuffer() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a new recipient list from RecipientsBuffer
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGFinishRecipientsBuffer()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishRecipientsBuffer()")
|
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
2006-12-14 05:51:22 -05:00
|
|
|
|
" go to buffer before doing work
|
|
|
|
|
if (bufnr("%") != expand("<abuf>"))
|
|
|
|
|
" switch to scratch buffer window
|
2011-07-05 06:45:44 -04:00
|
|
|
|
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-14 05:51:22 -05:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" 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
|
|
|
|
|
|
|
|
|
" get the recipients from the scratch buffer
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipients = []
|
|
|
|
|
let lines = getline(1,"$")
|
2008-07-29 08:29:28 -04:00
|
|
|
|
for recipient in lines
|
2009-06-08 08:32:13 -04:00
|
|
|
|
" delete all text after magic string
|
|
|
|
|
let recipient = substitute(recipient, s:GPGMagicString . ".*$", "", "")
|
|
|
|
|
|
2008-07-29 08:29:28 -04:00
|
|
|
|
" delete all spaces at beginning and end of the recipient
|
|
|
|
|
" also delete a '!' at the beginning of the recipient
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
|
2009-06-08 08:32:13 -04:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" delete comment lines
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipient = substitute(recipient, "^GPG:.*$", "", "")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" only do this if the line is not empty
|
|
|
|
|
if (strlen(recipient) > 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let gpgid = s:GPGNameToID(recipient)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
if (strlen(gpgid) > 0)
|
2011-07-05 06:46:39 -04:00
|
|
|
|
if (match(recipients, gpgid) < 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipients += [gpgid]
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
else
|
2008-07-30 10:48:53 -04:00
|
|
|
|
if (match(recipients, recipient) < 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipients += [recipient]
|
2011-07-05 03:35:33 -04:00
|
|
|
|
echohl GPGWarning
|
2011-07-05 06:46:39 -04:00
|
|
|
|
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
|
2011-07-05 03:35:33 -04:00
|
|
|
|
echohl None
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-08-04 02:27:59 -04:00
|
|
|
|
endif
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-07-29 07:03:08 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" 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.
|
2011-07-05 06:46:39 -04:00
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients)
|
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
|
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" check if there is any known recipient
|
2011-07-05 06:46:39 -04:00
|
|
|
|
if (len(recipients) == 0)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl GPGError
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom 'There are no known recipients!'
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echohl None
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-01-28 04:21:04 -05:00
|
|
|
|
|
|
|
|
|
" reset modified flag
|
|
|
|
|
set nomodified
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGViewOptions() {{{2
|
|
|
|
|
"
|
|
|
|
|
" echo the recipients
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGViewOptions()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewOptions()")
|
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
if (exists("b:GPGOptions"))
|
|
|
|
|
echo 'This file has following options:'
|
|
|
|
|
" echo the options
|
2008-07-28 17:01:41 -04:00
|
|
|
|
for option in b:GPGOptions
|
2002-04-24 04:59:12 -04:00
|
|
|
|
echo option
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGEditOptions() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a scratch buffer with all recipients to add/remove recipients
|
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGEditOptions()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditOptions()")
|
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
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
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let buffername = bufname("%")
|
|
|
|
|
let editbuffername = "GPGOptions_" . buffername
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" check if this buffer exists
|
2006-12-14 05:03:19 -05:00
|
|
|
|
if (!bufexists(editbuffername))
|
|
|
|
|
" create scratch buffer
|
2008-07-30 07:55:47 -04:00
|
|
|
|
execute 'silent! split ' . escape(editbuffername, ' *?\"'."'")
|
2006-12-14 05:03:19 -05:00
|
|
|
|
|
|
|
|
|
" add a autocommand to regenerate the options after a write
|
2008-01-28 04:21:04 -05:00
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
|
2006-12-14 05:03:19 -05:00
|
|
|
|
else
|
|
|
|
|
if (bufwinnr(editbuffername) >= 0)
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" switch to scratch buffer window
|
2011-07-05 06:45:44 -04:00
|
|
|
|
execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w"
|
2006-12-14 05:03:19 -05:00
|
|
|
|
else
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" split scratch buffer window
|
2008-07-30 07:55:47 -04:00
|
|
|
|
execute 'silent! sbuffer ' . escape(editbuffername, ' *?\"'."'")
|
2006-12-14 05:51:22 -05:00
|
|
|
|
|
2008-01-28 04:30:59 -05:00
|
|
|
|
" add a autocommand to regenerate the options after a write
|
|
|
|
|
autocmd BufHidden,BufUnload,BufWriteCmd <buffer> call s:GPGFinishOptionsBuffer()
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-14 05:03:19 -05:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" empty the buffer
|
|
|
|
|
silent normal! 1GdG
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Mark the buffer as a scratch buffer
|
2008-08-15 08:50:33 -04:00
|
|
|
|
setlocal buftype=nofile
|
2002-04-24 04:59:12 -04:00
|
|
|
|
setlocal noswapfile
|
|
|
|
|
setlocal nowrap
|
|
|
|
|
setlocal nobuflisted
|
|
|
|
|
setlocal nonumber
|
|
|
|
|
|
|
|
|
|
" so we know for which other buffer this edit buffer is
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let b:GPGCorrespondingTo = buffername
|
2002-04-24 04:59:12 -04: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 08: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.'
|
2002-04-24 04:59:12 -04:00
|
|
|
|
silent put ='GPG: ----------------------------------------------------------------------'
|
|
|
|
|
|
|
|
|
|
" put the options in the scratch buffer
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-28 17:01:41 -04:00
|
|
|
|
for option in options
|
2002-04-24 04:59:12 -04:00
|
|
|
|
silent put =option
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" 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
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
|
|
|
|
endif
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGFinishOptionsBuffer() {{{2
|
|
|
|
|
"
|
|
|
|
|
" create a new option list from OptionsBuffer
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGFinishOptionsBuffer()
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishOptionsBuffer()")
|
|
|
|
|
|
2006-12-13 10:28:31 -05:00
|
|
|
|
" guard for unencrypted files
|
|
|
|
|
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
|
|
|
|
|
echohl GPGWarning
|
2008-05-12 05:39:55 -04:00
|
|
|
|
echom "File is not encrypted, all GPG functions disabled!"
|
2006-12-13 10:28:31 -05:00
|
|
|
|
echohl None
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
|
2006-12-13 10:28:31 -05:00
|
|
|
|
return
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-13 10:28:31 -05:00
|
|
|
|
|
2006-12-14 05:51:22 -05:00
|
|
|
|
" go to buffer before doing work
|
|
|
|
|
if (bufnr("%") != expand("<abuf>"))
|
|
|
|
|
" switch to scratch buffer window
|
2011-07-05 06:45:44 -04:00
|
|
|
|
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2006-12-14 05:51:22 -05:00
|
|
|
|
|
2011-07-05 06:46:39 -04:00
|
|
|
|
" clear options and unknownOptions
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let options = []
|
|
|
|
|
let unknownOptions = []
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" 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
|
|
|
|
" get the options from the scratch buffer
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let lines = getline(1, "$")
|
2008-07-29 08:29:28 -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 05:34:34 -04:00
|
|
|
|
let option = substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" delete comment lines
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let option = substitute(option, "^GPG:.*$", "", "")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" only do this if the line is not empty
|
2011-07-05 06:46:39 -04:00
|
|
|
|
if (strlen(option) > 0 && match(options, option) < 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let options += [option]
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-07-29 07:03:08 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" write back the new option list to the corresponding buffer and mark it
|
|
|
|
|
" as modified
|
2011-07-05 06:46:39 -04:00
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options)
|
|
|
|
|
call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-01-28 04:21:04 -05:00
|
|
|
|
" reset modified flag
|
|
|
|
|
set nomodified
|
2010-01-19 04:58:57 -05:00
|
|
|
|
|
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-30 10:48:53 -04:00
|
|
|
|
" Function: s:GPGCheckRecipients(tocheck) {{{2
|
|
|
|
|
"
|
|
|
|
|
" check if recipients are known
|
|
|
|
|
" Returns: two lists recipients and unknownrecipients
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
2008-07-30 10:48:53 -04:00
|
|
|
|
function s:GPGCheckRecipients(tocheck)
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()")
|
|
|
|
|
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipients = []
|
|
|
|
|
let unknownrecipients = []
|
2008-07-30 10:48:53 -04:00
|
|
|
|
|
|
|
|
|
if (type(a:tocheck) == type([]))
|
|
|
|
|
for recipient in a:tocheck
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let gpgid = s:GPGNameToID(recipient)
|
2008-07-30 10:48:53 -04:00
|
|
|
|
if (strlen(gpgid) > 0)
|
|
|
|
|
if (match(recipients, gpgid) < 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let recipients += [gpgid]
|
2008-07-30 10:48:53 -04:00
|
|
|
|
endif
|
|
|
|
|
else
|
|
|
|
|
if (match(unknownrecipients, recipient) < 0)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let unknownrecipients += [recipient]
|
2008-07-30 10:48:53 -04:00
|
|
|
|
echohl GPGWarning
|
|
|
|
|
echom "The recipient \"" . recipient . "\" is not in your public keyring!"
|
|
|
|
|
echohl None
|
|
|
|
|
endif
|
|
|
|
|
end
|
|
|
|
|
endfor
|
|
|
|
|
endif
|
|
|
|
|
|
|
|
|
|
call s:GPGDebug(2, "recipients are: " . string(recipients))
|
|
|
|
|
call s:GPGDebug(2, "unknown recipients are: " . string(unknownrecipients))
|
|
|
|
|
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()")
|
2008-07-30 10:48:53 -04:00
|
|
|
|
return [ recipients, unknownrecipients ]
|
|
|
|
|
endfunction
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" Function: s:GPGNameToID(name) {{{2
|
|
|
|
|
"
|
|
|
|
|
" find GPG key ID corresponding to a name
|
|
|
|
|
" Returns: ID for the given name
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGNameToID(name)
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()")
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" ask gpg for the id for a name
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys \"" . a:name . "\""
|
|
|
|
|
call s:GPGDebug(2, "command: ". commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let output = system(commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
let &shell = s:shellsave
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(2, "output: ". output)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2011-07-05 02:57:47 -04:00
|
|
|
|
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
|
|
|
|
" so convert it, if necessary
|
2008-08-15 05:34:34 -04:00
|
|
|
|
if (&encoding != "utf-8")
|
|
|
|
|
let output = iconv(output, "utf-8", &encoding)
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let lines = split(output, "\n")
|
2011-07-05 02:57:47 -04:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" parse the output of gpg
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let pubseen = 0
|
|
|
|
|
let counter = 0
|
|
|
|
|
let gpgids = []
|
|
|
|
|
let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n"
|
2008-07-28 17:01:41 -04:00
|
|
|
|
for line in lines
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let fields = split(line, ":")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" search for the next uid
|
2011-07-05 06:46:39 -04:00
|
|
|
|
if (pubseen == 1)
|
2008-07-28 17:01:41 -04:00
|
|
|
|
if (fields[0] == "uid")
|
2009-06-08 08:32:13 -04:00
|
|
|
|
let choices = choices . " " . fields[9] . "\n"
|
2002-04-24 04:59:12 -04:00
|
|
|
|
else
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let pubseen = 0
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" search for the next pub
|
2011-07-05 06:46:39 -04:00
|
|
|
|
if (pubseen == 0)
|
2008-07-28 17:01:41 -04:00
|
|
|
|
if (fields[0] == "pub")
|
2009-06-08 08:32:13 -04:00
|
|
|
|
let identity = fields[4]
|
|
|
|
|
let gpgids += [identity]
|
|
|
|
|
if exists("*strftime")
|
|
|
|
|
let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n"
|
|
|
|
|
else
|
|
|
|
|
let choices = choices . counter . ": ID: 0x" . identity . "\n"
|
|
|
|
|
endif
|
|
|
|
|
let counter = counter+1
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let pubseen = 1
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" counter > 1 means we have more than one results
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let answer = 0
|
2002-04-24 04:59:12 -04:00
|
|
|
|
if (counter > 1)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let choices = choices . "Enter number: "
|
|
|
|
|
let answer = input(choices, "0")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
while (answer == "")
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let answer = input("Enter number: ", "0")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endwhile
|
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
|
2008-07-28 17:01:41 -04:00
|
|
|
|
return get(gpgids, answer, "")
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
|
|
|
|
" Function: s:GPGIDToName(identity) {{{2
|
|
|
|
|
"
|
|
|
|
|
" find name corresponding to a GPG key ID
|
|
|
|
|
" Returns: Name for the given ID
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGIDToName(identity)
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, ">>>>>>>> Entering s:GPGIDToName()")
|
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" TODO is the encryption subkey really unique?
|
|
|
|
|
|
|
|
|
|
" ask gpg for the id for a name
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let commandline = s:GPGCommand . " --quiet --with-colons --fixed-list-mode --list-keys " . a:identity
|
|
|
|
|
call s:GPGDebug(2, "command: ". commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredir
|
|
|
|
|
let &shell = s:shell
|
2011-07-05 06:51:12 -04:00
|
|
|
|
let output = system(commandline)
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let &shellredir = s:shellredirsave
|
|
|
|
|
let &shell = s:shellsave
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(2, "output: ". output)
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2011-07-05 02:57:47 -04:00
|
|
|
|
" when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8,
|
|
|
|
|
" so convert it, if necessary
|
2008-08-15 05:34:34 -04:00
|
|
|
|
if (&encoding != "utf-8")
|
|
|
|
|
let output = iconv(output, "utf-8", &encoding)
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let lines = split(output, "\n")
|
2011-07-05 02:57:47 -04:00
|
|
|
|
|
2002-04-24 04:59:12 -04:00
|
|
|
|
" parse the output of gpg
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let pubseen = 0
|
|
|
|
|
let uid = ""
|
2008-07-28 17:01:41 -04:00
|
|
|
|
for line in lines
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let fields = split(line, ":")
|
2011-07-05 06:46:39 -04:00
|
|
|
|
if (pubseen == 0) " search for the next pub
|
2008-07-28 17:01:41 -04:00
|
|
|
|
if (fields[0] == "pub")
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let pubseen = 1
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
2002-04-24 04:59:12 -04:00
|
|
|
|
else " search for the next uid
|
2008-07-28 17:01:41 -04:00
|
|
|
|
if (fields[0] == "uid")
|
2008-08-15 05:34:34 -04:00
|
|
|
|
let pubseen = 0
|
2009-06-08 08: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-28 17:01:41 -04:00
|
|
|
|
break
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
|
|
|
|
endif
|
2008-07-28 17:01:41 -04:00
|
|
|
|
endfor
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2010-01-19 04:58:57 -05:00
|
|
|
|
call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()")
|
2002-04-24 04:59:12 -04:00
|
|
|
|
return uid
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endfunction
|
2002-04-24 04:59:12 -04:00
|
|
|
|
|
2011-07-05 02:51:02 -04:00
|
|
|
|
" Function: s:GPGDebug(level, text) {{{2
|
|
|
|
|
"
|
|
|
|
|
" output debug message, if this message has high enough importance
|
2010-01-19 04:58:57 -05:00
|
|
|
|
" only define function if GPGDebugLevel set at all
|
2009-07-23 03:02:52 -04:00
|
|
|
|
"
|
2008-07-28 17:46:53 -04:00
|
|
|
|
function s:GPGDebug(level, text)
|
2010-01-19 04:58:57 -05:00
|
|
|
|
if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level
|
|
|
|
|
if exists("g:GPGDebugLog")
|
2011-07-05 06:51:12 -04:00
|
|
|
|
execute "redir >> " . g:GPGDebugLog
|
2010-01-19 04:58:57 -05:00
|
|
|
|
echom "GnuPG: " . a:text
|
|
|
|
|
redir END
|
|
|
|
|
else
|
|
|
|
|
echom "GnuPG: " . a:text
|
|
|
|
|
endif
|
2008-07-28 17:46:53 -04:00
|
|
|
|
endif
|
|
|
|
|
endfunction
|
2011-07-05 02:51:02 -04:00
|
|
|
|
|
2009-07-23 03:02:52 -04:00
|
|
|
|
" Section: Commands {{{1
|
|
|
|
|
|
2008-07-28 17:46:53 -04:00
|
|
|
|
command! GPGViewRecipients call s:GPGViewRecipients()
|
|
|
|
|
command! GPGEditRecipients call s:GPGEditRecipients()
|
|
|
|
|
command! GPGViewOptions call s:GPGViewOptions()
|
|
|
|
|
command! GPGEditOptions call s:GPGEditOptions()
|
2009-07-23 03:02:52 -04:00
|
|
|
|
|
2011-07-05 06:32:24 -04:00
|
|
|
|
" Section: Menu {{{1
|
2009-07-23 03:02:52 -04:00
|
|
|
|
|
2008-08-15 05:34:34 -04:00
|
|
|
|
if (has("menu"))
|
2011-07-05 06:32:24 -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-04-29 01:19:55 -04:00
|
|
|
|
|
|
|
|
|
" vim600: set foldmethod=marker foldlevel=0 :
|