#! /usr/bin/env runjs var argsjs = require('component-args.js'); var RFile = Packages.me.hatter.tools.commons.io.RFile; var main = () => { var parsedArgs = argsjs.parseDefARGs([]); var actions = ['desc', 'show', 'cat', 'edit', 'create']; var matchAction = (parsedArgs.length == 0)? []: actions.filter((a) => { return a.startsWith(parsedArgs[0]) }); if (matchAction == 0) { println('No or error args, usage:'); println(' enc [-k KEY_ID] desc|show|cat|edit|create'); return; } if (matchAction.length > 1) { println('Match more than one action: ' + JSON.stringify(matchAction)); return; } parsedArgs[0] = matchAction[0]; if (parsedArgs[0] == 'desc') { if (parsedArgs.length == 1) { println('File name is not assigned.'); return; } $$.shell().commands('pgpdump', parsedArgs[1]).inheritIO().run(); return; } var userDir = $$.prop('user.dir'); var pgpKeyId = parsedArgs.val('k'); if (pgpKeyId == null) { var encJSConf = $$.file($$.prop('user.home'), '.enc.js.conf'); if (!encJSConf.exists()) { println('Config file ~/.enc.js.conf not found.'); return; } var pgpKeyId = RFile.from(encJSConf).string().trim(); if (pgpKeyId.length == 0) { println('PGP key id is empty.'); return; } } if (parsedArgs[0] == 'create') { var fname; if (parsedArgs.length > 1) { fname = parsedArgs[1]; } else { print('Please input filename: '); fname = java.lang.System.console().readLine(); } if (fname.length == 0) { println('File name is not inputed.'); return; } var fn = fname.replace(/[^a-zA-Z0-9_.]/g, '_'); if ($$.file(userDir, fn).exists()) { println('File exists: ' + fn); return; } if ($$.file(userDir, fn + '.asc').exists()) { println('File exists: ' + fn + '.asc'); return; } requireJS('component-swing.js'); var txt = showWindow('Create - ' + fn, '', 'Input message:', true).get(); if (txt == null) { println('Message is not inputed.'); return; } RFile.from($$.file(userDir, fn)).write(txt); $$.shell().commands('sh', '-c', 'gpg -r ' + pgpKeyId + ' -e -a --no-comment --comment "https://hatter.in/key" ' + fn).start(); if ($$.file(userDir, fn + '.asc').exists()) { $$.file(userDir, fn).delete(); println('CREATE SUCCESS.'); } return; } var userDirFile = $$.file(userDir); var encFiles = $ARRAY(userDirFile.list()).filter((f) => { return (f.endsWith('.gpg') || f.endsWith('.asc')); }); if (encFiles.length == 0) { println('Can NOT find any .gpg or .asc file.'); return; } var argFilterEncFiles = parsedArgs.length > 1? encFiles.filter((f) => { return f == parsedArgs[1]; }): []; var selectFile = (argFilterEncFiles.length > 0)? argFilterEncFiles[0]: null; if (selectFile == null && encFiles.length > 1) { println('Please select one file:'); encFiles.forEach((f, i) => { println(' ' + i + ': ' + f); }); print('Input a number 0-' + (encFiles.length - 1) + ': '); var ln = java.lang.System.console().readLine(); var idx = 0; try { idx = java.lang.Integer.parseInt(ln); if ((idx < 0) || (idx >= encFiles.length)) { println("Index range error: " + idx); return; } selectFile = encFiles[idx]; } catch (e) { println('Parse error: ' + (e.getMessage == null)? e: e.getMessage()); return; } } if ((selectFile == null) && (encFiles.length == 1)) { selectFile = encFiles[0]; } if (selectFile == null) { println('File is not selected.'); return; } var result = $$.shell().commands('sh', '-c', 'cat ' + selectFile + ' | gpg').start(); var out = result[0].string(); var err = result[1].string(); if (err.contains('public key decryption failed')) { println('+ Decrypt file FAILED: ' + selectFile); if (!Packages.me.hatter.tools.jssp.main.StandaloneMain.JSSP_MAIN_MUTE) { println("ERROR detail:\n" + err); } return; } if (parsedArgs[0] == 'cat') { println('Enc message: ' + selectFile); println(repeat('-', 80)); println(out); println(repeat('-', 80)); return; } var listOnlyKeyIdOut = $$.shell().commands('gpg', '--list-only', '--list-packets', selectFile).start()[1]; var keyIds = $STR(listOnlyKeyIdOut).split('\n') .filter((x) => { return x.contains('ID '); }) .map((x) => { return x.replace(/.*ID\s+([0-9a-fA-F]+)[,\s]+.*/, '$1'); }); println('Encrypted key ids: [' + keyIds.join(', ') + ']'); requireJS('component-swing.js'); var actFLUC = parsedArgs[0].substring(0, 1).toUpperCase() + parsedArgs[0].substring(1); var txt = showWindow(actFLUC + ' - ' + selectFile, out, actFLUC + ' encrypted message:', parsedArgs[0] == 'edit').get(); if (txt != null) { // backup $$.file(userDir, selectFile).renameTo($$.file(userDir, selectFile + '.back')); // write text var txtFile = selectFile.substring(0, selectFile.length - 4) RFile.from($$.file(userDir, txtFile)).write(txt); $$.shell().commands('sh', '-c', 'gpg ' + keyIds.map((x) => {return '-r ' + x; }).join(' ') + ' -e -a --no-comment --comment "https://hatter.in/key" ' + txtFile).start(); if ($$.file(userDir, txtFile + '.asc').exists()) { $$.file(userDir, txtFile).delete(); println('UPDATE SUCCESS.'); } } else { println('BYE.'); } }; main(); java.lang.System.exit(0);