twokeys-server
Version:
Server for 2Keys
135 lines (129 loc) • 4.99 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
Copyright 2018 Kishan Sambhi
This file is part of 2Keys.
2Keys 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 3 of the License, or
(at your option) any later version.
2Keys is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with 2Keys. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @overview Handler for autohotkey running
*/
const fs = __importStar(require("fs"));
const util_1 = require("util");
const config_1 = require("./config");
const logger_1 = __importDefault(require("./logger"));
const path_1 = require("path");
const ahk = require("../../build/Release/twokeys");
const logger = new logger_1.default({ name: "ahk" });
const access = util_1.promisify(fs.access);
/**
* Fetches a hotkey from the config file,
* based on the keyboard and hotkey to find,
* providing the type of the hotkey,
* it's corrsponding function
* and the file with that function in.
*/
async function fetch_hotkey(keyboard, hotkey_code) {
const config = await config_1.config_loader();
// Get hotkey func
let func;
let type;
if (!config.keyboards.hasOwnProperty(keyboard)) { // Validate
throw new ReferenceError(`Keyboard ${keyboard} was not found!`);
}
else if (!config.keyboards[keyboard].hotkeys.hasOwnProperty(hotkey_code)) {
throw new ReferenceError(`Hotkey ${hotkey_code} was not found in keyboard ${keyboard}!`);
}
const hotkey = config.keyboards[keyboard].hotkeys[hotkey_code];
if (typeof hotkey !== "string") {
// Object type
func = hotkey.func;
type = typeof hotkey.type === "undefined" ? "down" : hotkey.type;
}
else {
func = hotkey;
type = "down";
}
// Get file
const file = path_1.join(process.cwd(), config.keyboards[keyboard].dir, config.keyboards[keyboard].root);
return {
type,
file,
func,
};
}
exports.fetch_hotkey = fetch_hotkey;
/**
* Run a hotkey by sending execution string to C++ addon
* @param file {String} File to get hotkeys from
* @param func {String} Function to run from that file
*/
async function run_hotkey(file, func) {
const old_cwd = process.cwd();
// 0: Set execution test
const exec_test = `
; AHK EXEC 2KEYS
; PRELUDE
Global TWOKEYS_CWD := "${process.cwd()}"
; GRAB CLIENT CODE
#Include ${file}
; EXECUTE
${func}()
`;
// 1: Santise file input to prevent code injection
// Check it exists
try {
await access(file, fs.constants.F_OK | fs.constants.S_IFREG);
}
catch (err) {
logger.throw_noexit(err);
}
finally {
// 2: Verify func ok
// Check if matches FuncName() format
const regexp = /^[a-z0-9]+$/i; // From https://stackoverflow.com/questions/388996/regex-for-javascript-to-allow-only-alphanumeric
if (regexp.test(func)) {
// Yay! run the hotkey
logger.debug(`#Include ${file}; ${func}()`);
try {
const userspace_config = await config_1.userspace_config_loader();
// Check the DLL config is present
if (typeof userspace_config.software.ahk.paths.dll === "undefined") {
logger.throw_noexit(new Error("DLL config option was not found! It may be an EXE file is only available, which isn't supported."));
return; // STOP execution
}
// Errors handled by the code
const ahk_run = ahk.run_ahk_text(path_1.join(userspace_config.paths.software, userspace_config.software.ahk.paths.root, userspace_config.software.ahk.paths.dll), exec_test);
// Change back to old CWD (DLL changes CWD)
process.chdir(old_cwd);
}
catch (err) {
logger.throw_noexit(err);
}
}
else {
logger.err(`Function ${func} is invalid for regexp "${regexp}"`);
}
}
}
exports.run_hotkey = run_hotkey;
//# sourceMappingURL=ahk.js.map