vis-gpg

edit GPG encrypted files in place with vis
git clone anongit@rnpnr.xyz:vis-gpg.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 60d7abbbf85a403da9ef71248d5ad6f6acdb045e
Author: Randy Palamar
Date:   Mon, 27 Mar 2023 06:52:19 -0600

initial import

Diffstat:
ALICENSE | 15+++++++++++++++
Avis-gpg.lua | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,15 @@ +ISC License (ISC) + +© 2023 Randy Palamar <palamar@ualberta.ca> + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/vis-gpg.lua b/vis-gpg.lua @@ -0,0 +1,60 @@ +gpg = { key = 0 } + +local function splitext(file) + if file == nil then return nil, nil end + local i = file:reverse():find('%.') + if i == nil then return file, nil end + return file:sub(0, -(i + 1)), file:sub(-i) +end + +local function decrypt(file) + local f, e = splitext(file.name) + if e ~= '.gpg' then return end + + local err, ostr, estr = vis:pipe(file, {start = 0, finish = file.size}, "gpg -d") + if err ~= 0 then return false end + + local i = estr:find("ID") + local j = estr:find(",", i) + local keyid = estr:sub(i+3, j-1) + if keyid ~= gpg.key then + vis:info(estr:gsub("\n[ ]*", " ")) + gpg.key = keyid + end + + file:delete(0, file.size) + file:insert(0, ostr) + file.modified = false + return true +end +vis.events.subscribe(vis.events.FILE_OPEN, decrypt) +vis.events.subscribe(vis.events.FILE_SAVE_POST, decrypt) + +local function encrypt(file, path) + local f, e = splitext(file.name) + if e ~= '.gpg' then return end + + if gpg.key == 0 then + vis:info('encrypt: keyid not found. file not saved.') + return false + end + + local tfn = os.tmpname() + local cmd = "gpg --yes -o " .. tfn .. " -e -r " .. gpg.key + local err, ostr, estr = vis:pipe(file, {start = 0, finish = file.size}, cmd) + if err ~= 0 then + if estr then + vis:message(estr) + end + return false + end + + local tf = io.open(tfn, 'rb') + file:delete(0, file.size) + file:insert(0, tf:read("a")) + tf:close() + os.remove(tfn) + + return true +end +vis.events.subscribe(vis.events.FILE_SAVE_PRE, encrypt)